Help with prompt() in function and using while with switch


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-qDwp0/1/4?curriculum_id=506324b3a7dffd00020bf661


What I am trying to acheive is to create a script that asks the User for 5 fruits to create a juice blend.

I put the opening question in a function askFruit because I want to ask that question several times and use the same variable.
Once the question is asked first, I want behavior to happen WHILE itemCount is less than 5.
For each case of the SWITCH I want to print a message about the User's choice, add 1 to the itemCount, and ask the question again.
After itemCount reaches 5, I want to print a final message to the console.

The error message I receive is

ReferenceError: item is not defined

which is vague and doesn't identify where the issue is. Although it just hit me now that it isn't talking about some generic item, it might be talking about the variable item.

I would appreciate some guidance on the error I've made and how I can think about coding this in a way that JS will understand what I'm trying to do. Thank you


var itemCount = 0
function askFruit() {
var item = prompt("Pick 5 fruits for this juice blend")
}

askFruit();

while(itemCount < 5){
    switch(item) {
        case 'apple':
            console.log("Oooh, apples are good");
            itemCount ++;
            askFruit();
            break;
        case 'orange':
            console.log("Ahh, the treasure fruit of Riverside");
            itemCount ++;
            askFruit();
            break;
        case 'kale':
            console.log("Ha, Google food");
            itemCount ++;
            askFruit();
            break;
        case 'mango':
            console.log("Ay ay ay ay!  Que tropical!");
            itemCount ++;
            askFruit();
            break;
        default:
            console.log("I don't know what that is but you're drinking it.");
            itemCount ++;
            askFruit();
            break;
    }

}

console.log("Mmmmm....now that's a tasty juice");


#2

Yes, the variable. With this little code you can easily narrow down where it is by inserting print statements. You can also run it in another interpreter to get a line number with the error message.

So at the time that was raised, had you created that variable in a place where it's accessible where the exception is happening? Possible explanations are having used a different name, having created it somewhere else, or not having created it at all.


#3

Your comment helped guide me in the right direction, thank you.

I realized that the item variable was defined outside of the WHILE
I also realized that if I listed the prompt inside the while, it would be asked repeatedly while itemCount was less than 5 so I was able to remove the calls to the function inside each CASE. I seemed to also not need the function so I took the prompt out of a function.

Now I can get the code to work, sort of.
I was hoping that the console.logs would appear between each prompt but all of the prompts seem to print after the 5th prompt. I could cater the text in each CASE to make up for this, but I would also like to see how I could make this appear as intended.
What would I have to do to cause the console.logs to be printed between each prompt?

Below is the new code

Thank you

var itemCount = 0
while(itemCount < 5){
	
	var item = prompt("Pick 5 fruits for this juice blend");

    switch(item) {
    case 'apple':
        console.log("Oooh, apples are good");
        itemCount ++;
        break;
    case 'orange':
        console.log("Ahh, the treasure fruit of Riverside");
        itemCount ++;
        break;
    case 'kale':
        console.log("Ha, Google food");
        itemCount ++;
        break;
    case 'mango':
        console.log("Ay ay ay ay!  Que tropical!");
        itemCount ++;
        break;
    default:
        console.log("I don't know what that is but you're drinking it.");
        itemCount ++;
        break;
}
}
console.log("Mmmmm....now that's a tasty juice");

#4

Actually that would have been fine, variables in surrounding scopes are accessible unless other variables with identical names hide them. It wasn't created in a surrounding scope though, it was created in a function, and after that function exited, its scope also exited and its variables were all destroyed. Even if it continued to exist, it would still have been inside the scope of the function, inaccessible from the outside.

As for the order between prompts and logs.. hardly matters as they are very different interfaces that you're unlikely to want to use together. It'd make more sense to only use the console (probably not possible to read from a browser's console, but command-line applications can), or only alerts/prompts, or read from / write to a html page


#5

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