10 posts in this topic

The comment basically has my issue, but I'm basically trying to fix my second var prompt so it will actually run and I can't figure it out. If anyone can help I would greatly appreciate it.

 

Line comment: var one above this comment isn't working, how do I fix it? I also need it to continue to the next else if statement once something is entered. I'm thinking of making it so that if it changes var input to 2 once it's entered then 3, will that work?

<!DOCTYPE html>
<html>
<head>
    <title>Project 1 – Michael Fiorello</title>
            <script>
            do{
        var input = prompt ("Please enter 1, 2, 3, or exit.");{
            if (input == "1")
                 {for
                    var one = prompt ("Please enter a string");
                  //var one above this comment isn't working, how do I fix it? I also need it to continue to the next else if statement once something is entered. I'm thinking of making it so that if it changes var input to 2 once it's entered then 3, will that work?
                    if (one != null)
                    else console.warn("You need to enter something")
                 }in(one !="")
                 //need the above "in" to cause it to loop this section until something is entered for var one
            else if (input == "2")
                {
                alert ("COOL!")
                }
            else if (input == "3")
                {
                alert ("AWESOME!")  
                }
            else if (input.toLowerCase() == "exit")
                {
                alert ("Okay") 
                }
            else 
                {
                alert ("Nope")
                console.warn("You need to enter something");
                }
                }
                }while(input != "exit");
        </script>
    </head>
    <body>

    </body>
</html>
Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

You have a stray "for" in the line preceding the prompt.

 

You should press F12 to open the console. it will point out Javascript errors like this one.

Share this post


Link to post
Share on other sites

Updated version. Thanks for the help earlier. I ended up falling back to an earlier version because I realized a for/while loop wasn't very useful for this program. Here is my current problem. I need to have the program loop through each program until the user enters something, but not sure what to put there. I put a comment in the problem area.

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
    <script>
    do{
    var input = prompt ("Please enter 1, 2, 3, or exit.");{
    if (input == "1")
         do{
             var one = prompt ("Please enter a string.");{
              if (one = null) { console.warn("You need to enter something");}
                 
    
                 }
                     
             }while (one != "")//I need this to be if the user enters nothing it will keep asking, right now it causes an infinite loop. What do I put here?
    else if (input == "2")
        {
        alert ("COOL!");
        }
    else if (input == "3")
        {
        alert ("AWESOME!"); 
        }
    else if (input == "exit")
        {
       alert ("Okay");
        }
    else 
        {
        alert ("Nope");
        console.warn("You need to enter something");
        }
        }
        }while(input != "exit");
</script>
</head>
<body>
<h1></h1>

</body>
</html>
Edited by TheShadowGamer

Share this post


Link to post
Share on other sites
if (one = null)
That's your problem.

Share this post


Link to post
Share on other sites
if (one = null)
That's your problem.

 

So how do I make it so it gives the console warning and still loop?

 

If I do this

do{
             var one = prompt ("Please enter a string.");{
              if (one = null) { console.warn("You need to enter something");}
                 
    
                 }
                     
             }while (one = null);

it still continues to the start menu even if nothing is entered.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

I know, because you are changing the value of one instead of checking the value. Compare that if statement with your other if statements.

Share this post


Link to post
Share on other sites

I know, because you are changing the value of one instead of checking the value. Compare that if statement with your other if statements.

I still have no clue what you mean or how to fix it. I tried changing it to == also.

Share this post


Link to post
Share on other sites

if you use a single '=' you are forcing variable one to equal null (one will equal null from now until changed again), when in a if condition you should be comparing using '==', or '===' IF one is EQUAL to null (one remains its current value and only compares to identify if it equals null)

Share this post


Link to post
Share on other sites

This page has an equivalency table for Javascript:

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

 

For a loose comparison, null is only equivalent to null and undefined. For a strict comparison, null is only equivalent to null. Prompt does not return null or undefined, it returns a string (an empty string if they didn't type anything in). So that's why comparing to null didn't work either.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now