Jump to content



Photo

my first script


  • Please log in to reply
9 replies to this topic

#1 damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 05 February 2014 - 13:30

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




#2 Haggis

Haggis

    Neowinian Senior

  • Tech Issues Solved: 12
  • Joined: 13-June 07
  • Location: Near Stirling, Scotland
  • OS: Debian 7
  • Phone: Samsung Galaxy S3 LTE (i9305)

Posted 05 February 2014 - 13:44

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



#3 OP damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 05 February 2014 - 13:50

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


#4 the better twin

the better twin

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 26-January 10
  • OS: Win 8
  • Phone: Nokia Lumia

Posted 05 February 2014 - 13:54

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')


#5 OP damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 05 February 2014 - 14:05

 

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 



#6 the better twin

the better twin

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 26-January 10
  • OS: Win 8
  • Phone: Nokia Lumia

Posted 05 February 2014 - 14:09

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. 



#7 OP damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 05 February 2014 - 14:23

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



#8 OP damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 05 February 2014 - 18:02

this cant be done how would you do it?its a stupid idea though I did learn something facinating stuff really cant wait to learn more



#9 Clearskies

Clearskies

    Neowinian

  • Joined: 10-November 11
  • Location: Ottawa, Canada
  • OS: W8 / Xubuntu
  • Phone: Samsung Galaxy SII X

Posted 07 February 2014 - 15:00

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);


#10 OP damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 11 February 2014 - 09:42

OK I have pasted it and will try to make it work ty