• 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 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 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
https://www.neowin.net/forum/topic/1199295-my-first-script/
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

  • 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
  • 0
  On 05/02/2014 at 13:54, the better twin said:

 

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 

  • 0
  On 05/02/2014 at 14:05, damindor said:

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. 

  • 0
  On 05/02/2014 at 14:09, the better twin said:

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

  • 0
  On 05/02/2014 at 14:23, damindor said:

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);
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Download Apple macOS 26 Tahoe, iOS 26 official stock wallpapers in high quality by Aditya Tiwari Apple's latest software design can be thought of as the tech version of 'new year, new me.' macOS 26 is one among them, featuring the "Liquid Glass" as a translucent new material that reflects and refracts its surroundings. The updated macOS design is all over the place, including the Dock, sidebars, and toolbars, which have been refined to focus more on the user's content. Apple continued its annual tradition and introduced new wallpapers custom-made for macOS 26 Tahoe to go along with the new design language. These macOS Tahoe 26 wallpapers are available in light and dark theme options, complementing the transparent menu bar, which makes the display feel bigger. To download the wallpapers to your device, click on the image to open it, then right-click on the wallpaper and select "Save Image As." Apple said during the announcement that "the new design also unlocks more personalization on the Mac. App icons come to life in light or dark appearances, colorful new light and dark tints, as well as an elegant new clear look." Apple's Liquid Design-inspired default wallpapers are also available for iOS 26 in light and dark options. The company has utilised Liquid Design extensively when upgrading the wallpaper experience on iPhones. Lock Screen wallpapers on iPhone create a 3D effect when the device is moved around, giving the illusion that the objects in the image are popping out of the screen. The time displayed on the lock screen fluidly adapts to the available space in an image for a more dynamic feel. Not just the design, Apple has further bridged the gap between iPhone and Mac by adding new Continuity features to macOS 26 on these supported Mac models. This includes the new Phone app that lets you relay phone calls from your iPhone nearby. Just like widgets, macOS 26 can populate Live Activities from a nearby iPhone, enabling you to track your Uber ride, live sports scores, or incoming dinner orders. Source: Apple via 9to5Mac [1,2]
    • Whatever, you aimless bunch at M$ want to keep on wasting time and resources into applying makeup into that pig - I'll keep Open-Shell or Startallback thank you for nothing and be gone.
    • Maybe, I can’t quite recall. I skipped 8. My recollection is only it went from full screen to the option to not be full screen? It’s hazy lol
    • 8 to 8.1 was a pretty big overhaul, wasn’t it? 8.1 wasn’t really a new version of Windows. Is that what you were referring to in the tinkering comment?
  • Recent Achievements

    • Explorer
      MusicLover2112 went up a rank
      Explorer
    • Dedicated
      MadMung0 earned a badge
      Dedicated
    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      254
    4. 4
      +Edouard
      202
    5. 5
      snowy owl
      169
  • Tell a friend

    Love Neowin? Tell a friend!