Exercise 13 Conclusion


#1

Hi!

First post here.

What is wrong with my code? I tried creating a game in which a user has a choice of visiting two cities - Moscow and Bogota where he would meet people. For some reason, computer shows I meet people from Moscow only no matter what input I give in the questionnaire. What am I doing wrong here? Thanks!

var city = prompt("What city do you want to go to - Moscow or Bogota?");
if (city = "Moscow") {
var girlsNames = ["Sasha", "Ira"];
for (i=0; i console.log ("You will meet " + girlsNames[i]);
}
else {
var girlsNames1 = ["Claudia", "Tamara"];
for (i = 0; i < girlsNames1.length; i++)
console.log ("You will meet " + girlsNames1[i])
}


#2

Use comparison operator:

if (city === "Moscow")

It's not necessary to declare a variable more than once. If you expect to assign different values to a variable, declare it at the beginning of your code.

var city, girlsNames, i;

city = prompt(" ... ");

then in your if statements,

girlsNames = ["...", "..."];

The above is messed up pretty good. I'm guessing you want something like this...

for (i = 0; i < girlsNames.length; i++) {
    console.log ("You will meet "  + girlsNames[i]);
}

Needs an opening brace...

for ( ... ) {

#3

Hi Mtf,
Thanks so much for your feedback. I fixed the code and now it works. I still have a question.

What is the difference between "=" and "===" in the conditional if... else statement?

In the Justin Bieber game, from Lesson 1, they use "=" for the conditional statement. i.e. if (useranswer = "yes")

var userAnswer=prompt("Do you want to race Bieber on stage?")
if(useranswer = "yes")
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!")
}
else
{console.log ("Blah Blah")
}

However, in the rock, paper, scissors game they use "===", i.e. if (choice2 === "scissors")

...
var compare = function (choice1, choice2) {
if (choice1 === choice2){
return "The result is a tie!";
}
else if (choice1 === "rock") {
if (choice2 === "scissors")
{
return "rock wins";
}
else
{
return "paper wins";
}
}

....


#4

The first can only be used when makin an assignment.

var a = 42;

The other must be used when making a comparison (a boolean expression).

    a = 5;
    b = 5;
    c = a === b;
    console.log(c);    // true

The example you give, if (userAnswer = 'yes')` is invalid code and will throw an error.


#5

This makes sense. Thank you.

I just realized there was an error in the Justin Bieber game.


#6

This topic was automatically closed after 24 hours. New replies are no longer allowed.