• 0

my first script


Question

so I have a few questions here is the script

<body>
<script type="text/javascript" language="javascript">
x=prompt('please enter any amount of money you want.','500000')
if(x>=5)
{alert('you are very greedy please enter any amount less then 5 go wild the sky is the limit')
}
else{document.write('congratz you won' + x)
}
</script>
</body>

Now I want the document write function to say

congratz you won x dollars but I dont now how to do that if I do

(document.write('congratz you won' +x 'dollars') it does not work

also the alert box pops only once how do i get it to pop again some loop right because if the user continues to enter a number >5 he will have to continually try again

Please help this might be silly but its to learn at the end of the day

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I dont know a lot about javascript so you may need to make changes

 

have a look at this see if it helps

 

basically you want to DO the same thing until your answer is less than 5

do{
    var selection = parseInt(window.prompt("please enter any amount of money you want.", ""), 10);
}while(selection > 4);

so this would continuously ask for input until u enter a number less than 5

Link to comment
Share on other sites

  • 0

OK thanks I tried a while loop like this

 

<script type="text/javascript" language="javascript">
x=prompt('please enter any amount you want.','5000')
while(x>=5)
prompt('you are too greedy please enter any amount less then 5.','4')
</script>
 
and got a perpetual loop I will try a do while loop like you suggested
Link to comment
Share on other sites

  • 0

You need to concatenate again after the variable and dont forget the spaces. In Javascript you use + as you already have done. 

document.write('congratz you won ' + x + ' dollars')
Link to comment
Share on other sites

  • 0

 

You need to concatenate again after the variable and dont forget the spaces. In Javascript you use + as you already have done. 

document.write('congratz you won ' + x + ' dollars')

OK cool I am having trouble getting the loop though watching a video on the do while loop trying to get a handle on it 

Link to comment
Share on other sites

  • 0

OK cool I am having trouble getting the loop though watching a video on the do while loop trying to get a handle on it 

Its effectively the same as a while loop but it will always run that one condition at least once. 

Link to comment
Share on other sites

  • 0

Its effectively the same as a while loop but it will always run that one condition at least once. 

I noticed so you get a prompt box to enter the amount you want even if its less then 5 the code will be excecuted at least once even if the value is >5

Link to comment
Share on other sites

  • 0

I noticed so you get a prompt box to enter the amount you want even if its less then 5 the code will be excecuted at least once even if the value is >5

 

With a do/while loop, the code inside the loop will always get executed at least once. To make it work the way you want, you would only prompt the user inside the loop, and also check if the value is acceptable inside the loop. This code should give you an idea of what you should be doing:

var x = 10;
do {
    x = parseInt(prompt("Please enter any amount of money you want", "5000"));
    if (x >= 5) {
        alert("You are very greedy. Please enter any amount less than 5.");
    }
} while (x >= 5);

document.write("Congrats, you won $" + x);
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.