0

I'm a beginner on JavaScript so can someone tell me why this doesn't work?

<!doctype html>
<head>
<title>Assignment</title>
</head>

<script>
var name = ("John" & "Penny" & "Pat")
name = prompt("What is the student name?");
if (name="John" && "Penny" && "Pat")
{
document.write(name + " has one free hour of lessons.");
}
else
{
document.write(name + "doesn't have any free lessons.");
}
</script>

It will prompt me with the question but despite what I input it will always output with "Pat has one free hour of lessons." Anyone help me?

4 Answers4

4

You're using the wrong equals here:

if (name="John" && "Penny" && "Pat")  

Change it to

if (name === "John" || name === "Penny" || name === "Pat")

You're also setting name incorrectly:

var name = ("John" & "Penny" & "Pat")

should be

var name = ["John", "Penny", "Pat"];

(if you want an array.) (Though you're overwriting it in the next line, so why ever set it like that in the first place?).

Finally, it's bad practice to use document.write, try console.log instead.

To sum up so far:

var name = prompt("What is the student name?");
if (name === "John" || name === "Penny" || name === "Pat") {
    console.log(name + " has one free hour of lessons.");
} else {
    console.log(name + " doesn't have any free lessons.");
}

As noted in the comments, this gets difficult when you add more users. Instead, let's store the users who have a free hour in an array (thus, adding new students is easy, just add them to the array):

var haveFreeHour = ["John", "Penny", "Pat"];

Then, we can use indexOf in the if statement:

var haveFreeHour = ["John", "Penny", "Pat"];
var name = prompt("What is the student name?");
if (haveFreeHour.indexOf(name) > -1) {
    console.log(name + " has one free hour of lessons.");
} else {
    console.log(name + " doesn't have any free lessons.");
}

Play with this at JSFiddle

Community
  • 1
  • 1
SomeKittens
  • 35,809
  • 19
  • 104
  • 135
  • Comparing all elements of an array in a single if statement with ===, very bad practice! – aneelkkhatri Apr 02 '14 at 18:35
  • @aneelkkhatri What array, where? You'll notice there isn't an array in the final code. – SomeKittens Apr 02 '14 at 18:36
  • That's even worse! What if you've got 1000 names, and you're gonna put all of them in a single if statement??? – aneelkkhatri Apr 02 '14 at 18:38
  • 1
    Please @aneelkkhatri, inform me why `===` is bad practice? Please.. I would love to hear this. – rlemon Apr 02 '14 at 18:39
  • I meant putting 1000 === conditions in single if statement is bad! – aneelkkhatri Apr 02 '14 at 18:41
  • 1
    @aneelkkhatri I don't see *any* `===` conditionals in my if statement. – SomeKittens Apr 02 '14 at 18:43
  • This answer is outlining the core of the issue, which is the conditional statement in the IF. It provides an answer in its most basic of forms. It does not attempt, nor should it, to be a canonical answer. – rlemon Apr 02 '14 at 18:43
  • if (name === "John" || name === "Penny" || name === "Pat") doesn't it have ===?? – aneelkkhatri Apr 02 '14 at 18:45
  • @aneelkkhatri ...but that's not my final answer! – SomeKittens Apr 02 '14 at 18:47
  • so if you've got 1000 names, you would do something like this: if(name === "name0" || name === "name1" || name === "name2" || name === "name3" || name === "name4" || name === "name5" || name === "name6" || name === "name7" || name === "name8" || .... || name === "name999" ) { ... } ???? – aneelkkhatri Apr 02 '14 at 18:52
  • 2
    @aneelkkhatri that is not at all what he is advocating. However for the purposes of explaining the answer it is important to show the OP (imo) that step in the thinking process. Read the answer to the finish and you will see he offers a better solution. – rlemon Apr 02 '14 at 18:56
  • Thankyou for the in depth reply but I don't know why "console.log" isn't working. Is it because I'm doing this in a notepad and opening it into a browser? That's my only option when completing this assignment as it must be done in wordpad/notepad – user3490706 Apr 02 '14 at 21:09
  • @user3490706 Ouch, there are *so* many better options than notepad. As an instructor of programming myself, I'd recommend learning good tools. On topic, what browser are you using? `f12` usually opens the console. – SomeKittens Apr 02 '14 at 21:11
0

You need to use == or === for comparison.

Here is more info on what each one does http://www.2ality.com/2011/06/javascript-equality.html

Strict equals === [ES5 11.9.6] Comparing two values. Values with different types are never equal. If both values have the same type then the following assertions hold.

Equals == [ES5 11.9.3] Comparing two values. If both values have the same type: compare with ===. Otherwise: undefined == null One number, one string: convert the string to a number A boolean and a non-boolean: convert the boolean to a number and then perform the comparison. Comparing a string or a number to an object: try to convert the object to a primitive and then make the comparison.

Also your names variable should be used as an Array and a different one for your prompt, something like :

var names = ["John", "Penny", "Pat"];
var name = prompt("What is the student name?");

Then instead of comparison you could use indexOf (more on indexOf) to see if your value is in your array, something like :

if(names.indexOf(name) > -1)...
Gabriel
  • 327
  • 4
  • 13
0

Here's what you want:

<!DOCTYPE html>
<head>
    <title>Assignment</title>
</head>
<body>
    <script type="text/javascript">
        var names = ["John","Penny","Pat"];

        var name = prompt("What is the student name?");

        if(names.indexOf(name) > -1) 
        {
            console.log(name + " has one free hour of lessons.");
        }
        else 
        {
            console.log(name + " doesn't have any free lessons.");
        }
    </script>
</body>
</html>


P.S. You should avoid using document.write. I have edited the code and changed that part to console.log

aneelkkhatri
  • 394
  • 2
  • 10
-3

You need to use a double == for conditionals.

Smeegs
  • 8,796
  • 5
  • 34
  • 71
  • 2
    Use **triple** === for conditionals. – Malvolio Apr 02 '14 at 18:11
  • 3
    you only need to use a triple if you're comparing type and value. – Smeegs Apr 02 '14 at 18:12
  • 3
    @Smeegs -- it's bad policy to use == in general. It's slower than ===, FWIW, and its behavior is non-intuitive. === does what you typically means by "equals". – Malvolio Apr 02 '14 at 18:27
  • @Malvolio, my answer isn't incorrect, and thus doesn't deserve to be downvoted. I'm not saying you're wrong, but I'm not wrong either. – Smeegs Apr 02 '14 at 18:59
  • `==` is never necessary and obscures the intent of your code. See http://dorey.github.io/JavaScript-Equality-Table/ for a helpful table showing the trickiness of `==`. – 1983 Apr 02 '14 at 19:01
  • @PotPlant, I understand. I'm not arguing against you. But my answer was to show that the conditional statements were incorrect. Not to debate the merits of `===` and `==`. You have to keep this answer in context. Using the comments to discuss this is a good thing, but downvoting a correct answer is counter productive. – Smeegs Apr 02 '14 at 19:03
  • @PotPlant, also, sometimes you do want a type converting comparison. Never say "never". – Smeegs Apr 02 '14 at 19:04
  • @Smeegs 1. Yep, you spotted the main problem, but I think it's best to start a beginner off on the right foot. 2. You may indeed want to coerce types during a comparison, but it is never necessary to do so. Using `===` avoids having to memorise this algorithm: http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3 It also prevents someone reading your code from worrying about whether you understood that algorithm correctly. – 1983 Apr 02 '14 at 19:14
  • @PotPlant, I agree about the right foot. Which is why I said these discussions are good. I would be happy to update my answer with more information. My point is, people like you who downvote correct answers make it so people like me don't want to answer. Both of us are on the same team, we are trying to help people understand more. But the first two comments here don't actually help the OP. They'll just confuse them. Our conversation at the bottom is helpful to anybody who will read this. Which is nobody because my answer was grayed out. Do you see my point? – Smeegs Apr 02 '14 at 19:19
  • @Smeegs I downvoted because 'use ==' is bad advice. The best way to avoid confusion is to say 'use ==='. More info: http://speakingjs.com/es5/ch09.html#equality_operators If you add some more information to your answer I'll reverse my downvote. – 1983 Apr 02 '14 at 19:27
  • @PotPlant, I give up. You're too wrapped up in yourself to see past your own nose. You are what's wrong with developers. Too eager to prove that you're right than to actually be helpful. Whatever. I fully expect you to google and post another link that will make you feel more superior. You should be very proud of yourself. – Smeegs Apr 02 '14 at 19:30
  • @Smeegs It's called *peer review*. It's why SO has high quality searchable content. – 1983 Apr 02 '14 at 19:34
  • @PotPlant, I KNOW, as I said over and over and over again. "Using the comments to discuss this is a good thing" and "Our conversation at the bottom is helpful". Are you doing this on purpose? At this point, I think you're just being belligerent on purpose. – Smeegs Apr 02 '14 at 19:38