简体   繁体   中英

Very simple JavaScript code doesn't work?

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?

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

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)...

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>


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

您需要对条件使用双==

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM