Error in PAY variable for Code Your Own Adventure 2


#1


Code Your Own Adventure 2


IMO, this code has an error. At the initial prompt, if you choose 'PAY', you are then followed up by two additional questions. The first question asked if you have money. The second asks if it's in troll dollars. It seems like there should be an if statement within the if statement so that you're not asked the question "Is your money in Troll Dollars?" if you have already replied that you do not have any money.

It should be:


case 'PAY':
    var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
    if(money === 'YES'){
    var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();    
      if (dollars === 'YES'){
        console.log("Great! You pay the troll and continue on your merry way.");
      }
      else {
      console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
      }
      break;


#3

I completely disagree.

Did you read my post as to why I don't agree? This was not a post saying my code isn't working. It was a post stating that I believe the example given by codecademy is not right.


#4

here:

if(money === 'YES'){

where do you close this if statement? I understand the nesting, but you need a } to close the if statement


#5

You're right, I was missing the closing bracket.

But you do agree that the Troll Dollars inquiry should only be triggered if the first question to money was answered yes?


#6

You can achieve by nesting if statements.

Then its important to close if(money === 'YES') after if (dollars === 'YES')


#7

Maybe I'm not being clear with my question/statement...

In the first section of "Code Your Own Adventure 2", the "What you'll be building" section where it shows the default code, there are consecutive questions about money, regardless of if the answer to the first question is "NO".

My suggestion is that the default code only prompts the user inquiry about Troll Dollars IF the answer to the first statement is "YES". If the answer is "NO" then there is no reason the computer should ask further, non-relevant questions.

Let me know if I've been clear.


#8

point still stands:

if (condition){
   if (condition){
   }
}

that you can use nested if statements.


#9

I understand how to achieve this properly.

My question is why does the default code erroneously proceed to question two if the answer to question 1 is no? If I ask if you own a car, and you reply "NO", why would I then ask, "What year is it made?"

The purpose of my post is simply to state that I believe that the nesting if statements which you have advised should be part of the code provided by codecademy.

The code that is shown now provides an unrealistic situation.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.