How come the last condition is executing, instead of the first ? Why isn't the while loop working ?

document.prompt("hello there")
if (i==2 || i==4 ) {
document.write("smart");
while(i>6){
} if(i=8){
document.prompt("can you ask me again");
}}else (i>=9){
document.write("you are a lemon");
}
//document.write(" I want to buy an apple ");

The flow of your sample code doesnt make sense..also, there's a window.prompt() method, but not a document.prompt() method that I'm aware of.

What are you actually trying to accomplish?

here is an example of how to use a while loop.

var x = 0;
while (x < 10)
{
  document.write("The number is " + x + "<br />");
  x++;
}

Probably the wrong loop.
I want to nest a loop within a condition, but when I run the condition, the last condition is executed, the first condition ?

The looping and condition was totally confusing. can you send full code?.

I created a new condition, although there are no errors, it's not working ?

// JavaScript Document
if (pizzatopings=="pineapple") {
var toppings = document.prompt("What is your favorite topping?");
if (toppings==apple) {
alert("no one puts apples on a pizza")
}else (toppings==chocolate) {
alert("chocolate on a pizza?");
}
}

I realized I'd have to nest the condition within the loop to answer my other question :)

You have a few sytnax errors, using variable names where string text should be, dont know what document.prompt is...should be window.prompt().

here is a working example...

<!DOCTYPE html>
<html>
<head>
 <title>Demo</title>

 <script>
 var pizzatopings = "pineapple";

 if (pizzatopings=="pineapple") {
   var toppings = window.prompt("What is your favorite topping?");
   if (toppings=="apple") 
   {
     alert("no one puts apples on a pizza")
   } 
   else if(toppings=="chocolate") 
   {
     alert("chocolate on a pizza?");
   }
   else
   {
     alert("default response... ");
   }
 }
 </script>

</head>
<body>


</body>
</html>

This code works except, if I type in steel at the prompt it displays the goat condition (I know the code is strange, I make up anything when I learn) :)

     if  (toppings == chocolate ) {
            var toppings = prompt("you like chocolate on your pizza");
    if (toppings == steel) {
    prompt("you like steel on a pizza");
    } else if (toppings == goat) {
    prompt("you like that"); 
    }
    else (toppings == metal) { 
    ("wrong answer"); 
        }
    }

unless chocolate, steel, goat, and metal are variable names, you need to place quotes around those strings. Look at the example I provided above. That should help you learn the flow of conditional statements.

All names within conditions should be strings.

JSBin is giving me 10 Errors ! My offline Javascript Editor fires no errors.

<script>
if (toppings == "chocolate" ) {
var toppings = prompt("you like chocolate on your pizza");
if (toppings == "steel") {
prompt("you like steel on a pizza");
} else if (toppings == "goat") {
prompt("you like that");
}
else (toppings == "metal") {
("wrong answer");
}
}
</script>

I found an editor which is pointing me out to an error that will cause me to solve this. It is saying there is a missing identifier, therefor the condition skips to the else statement ignoring the steel and goat conditional statements, the past four hours I've been wondering why it's skipping, atleast I have a hint. I remember how to write arrays faster then conditions :(

if (toppings == "chocolate" ); {
var toppings = prompt("you like chocolate on your pizza");
}if (toppings == 'steel'); {
prompt("you like steel on a pizza");
} else if (toppings == 'goat'); {
prompt("you like that");
}
else (toppings == 'metal'); {
("wrong answer");
}

I really do not know what you are doing with that messed up code????
Now look at my code then look at your code. My code works yours doesn't!

var toppings = prompt("What would you like on your pizza?");

      if (toppings == 'chocolate' ) {
        prompt('you want chocolate on your pizza?');
      } else if (toppings == 'steel') {
        prompt('you like steel on a pizza?');
      } else if (toppings == 'goat') {
        prompt('you like that?');
      } else {
        prompt('wrong answer');
      }

I'm trying to figure it out, nothing wrong with making mistakes, although it's becoming making mistakes is a sin.

The book I'm reading, tells me to put the prompt inside a variable, run the script.

if (toppings == 'peppers') {
var top = prompt('What is your favorite toppings?');
if (top == 'olive') {
alert('you like olives');
} else if (top == "steel") {
prompt('whatever');
} else {
prompt('gold person');
}
}

Freon - Your Code isn't working either, it won't execute.

This code works, except if I type in 'tuesday' at the prompt it gives me the final condition, instead of 'you like tuesday' ?

if (dayOfWeek == monday) {
var day = prompt('do you like the weather');
if (day == tuesday) {
prompt('you like tuesday'); 
} else if (day === thursday) {
prompt('you like this day');
} else {
prompt('that is ok');
}
}

Ok there is nothing wrong with making mistakes. We all make them and even when you get better at this you will still make mistakes. Atlease I still make them! So lets walk through your code.
You said the book you are reading said to put the prompt in a variable? What a prompt does is to return your answer to the code. If you look at my code you will see that YES I did use a variable ( var toppings = prompt("What would you like on your pizza?"); ) So the variable toppings will now hold my answer I then test the answer with my "if" statements to see if I can find a match so I can then display a prompt with my respones.

Now let look and see how you messed up my code! The below code is not what I had posted.

if (toppings == 'peppers') {
var top = prompt('What is your favorite toppings?');
if (top == 'olive') {
alert('you like olives');
} else if (top == "steel") {
prompt('whatever');
} else {
prompt('gold person');
}
}

This is what I posted.

var toppings = prompt("What would you like on your pizza?");
      if (toppings == 'chocolate' ) {
        prompt('you want chocolate on your pizza?');
      } else if (toppings == 'steel') {
        prompt('you like steel on a pizza?');
      } else if (toppings == 'goat') {
        prompt('you like that?');
      } else {
        prompt('wrong answer');
      }

You can see my variable toppings and you can see how I am test the answer.

Now lets look and see why your last posted code dose not work.

if (dayOfWeek == monday) {
var day = prompt('do you like the weather');
if (day == tuesday) {
prompt('you like tuesday'); 
} else if (day === thursday) {
prompt('you like this day');
} else {
prompt('that is ok');
}
}

So the first problem you have is your variables. You first test (dayOfWeek) to see if it equals "monday" now remember someone told you that strings have to be put inside of quotes "" or ''. That is your first problem your next problem is if dayOfWeek does equal "monday" then you will get a prompt that says "do you like the weather" and then the rest of your code will run. But if you do not say "monday" then your code will STOP. The reason is that you are not creating the variable day unless the variable dayOfWeek equals "monday".

Now you can make your code work but your if statement will need to be reworked some. I am sorry if this post is long but I wanted to show you where you are making some of your mistakes.

What a if statement does is to test a condition if that condition is true it will continue. But if that condition is false then it will test an else if condition if you have one and if that condition is also false it will run a else as the fallback if you have a else statement.

So looking at the flow of your last code the if statement evaluates to see if (dayOfWeek == 'monday') if it does it is true and will then run. But if it does not then it is false and will STOP because you have no else if and no else statement as a fallback to that if statement. I know you are going to look and say but I do have else if and I have a else as a fallback. But you really don't. Because your first if statement has no else if or else, you did incase another if statement that does have a else if and a else fallback but it will not run unless (dayOfWeek == 'monday') equals true.

With this condition, the prompt happens, then the prompt within the condition happens, if I enter in tuesday for the for the second prompt I get 'you like this day' instead of 'you like tuesday' ?

prompt('what do you like');
if (dayOfWeek == monday) {
var day = prompt('do you like the weather');
if (day == tuesday) {
prompt('you like tuesday'); 
} else if (day === thursday) {
prompt('you like this day');
} else {
prompt('that is ok');
}
}
// JavaScript Document

var select  = prompt("tell me your lucky number");
if (lucky == 7) {
var selection = prompt("what is your lucky number");
if (selection == 4) {
prompt("four is not such a lucky number");
} else if (selection == 3) {
prompt("why would you even choose three");
} else {
prompt("you have choosen that");
}
}

I made something that makes more sense.

Although the first prompt will appear but it won't check the condition because it's not within the condition, and I can't understand why the second prompt is not executing ?

The second prompt is nested within the condition it should run, but it's not ? Arghh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.