• 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

    • It's very pretty, IMO. I've always loved the Windows 7-style of UI design. 7 was the prettiest Windows ever. I'd love Microsoft to be THIS consistent and I'd love them to return to that.
    • Here are the Macs that support macOS 26 Tahoe by Taras Buria In addition to iOS 26, iPadOS 26, watchOS 26, and tvOS 26, Apple announced macOS 26 Tahoe, a new desktop operating system. Like the other operating systems made in Cupertino, macOS Tahoe features a redesigned UI with Liquid Glass, translucent icons, and other visual upgrades. There are also plenty of new features, such as a new Phone app for Mac, Live Activities on Mac, a massive Spotlight update, and a lot more. However, not all Mac owners will get to enjoy macOS 26. Like with iOS and iPadOS, Apple is dropping a few Macs with the release of macOS 26. You will be able to update to the new operating system if you own one of the following Apple computers. MacBook Air M1 and newer MacBook Pro M1 and newer MacBook Pro 13-inch 2020 (four Thunderbolt 3 ports) and newer MacBook Pro 16-inch 2019 iMac 2020 and newer Mac mini 2020 and newer Mac Studio 2022 and newer Mac Pro 2019 and newer Note that although Apple still supports Intel-based Macs, not all features will be available on systems that do not have Apple Silicon chips. Now-unsupported Macs that cannot upgrade from macOS Sequoia to macOS Tahoe are the following computers with Intel processors: Intel-based iMac 2019 Intel-based Mac mini 2018 iMac Pro Intel-based MacBook Air 2020 Intel-based MacBook Pro 2018 macOS 26 Tahoe is now available for developers to try in the beta program. Apple plans to launch the first public beta in July, alongside the release of public betas of iOS 26, iPadOS 26, and other operating systems that the company announced earlier today. You can find all the new features in macOS 26 Tahoe on the official Apple website.
    • I hope Microsoft goes back to the Fisher Price XP look first.
    • >It is a fundamental change to how macOS looks and feels, which some Neowin readers are definitely not a fan of. Hehe. You link to one of my posts here, but I actually LOVE glass effects and have spent the last decade or so hacking Windows to be more like Vista's Aero Glass, etc. Not sure why you thought my post implied I didn't like it. I was referring more to Apple copying this from Windows. I wish MS would being more glass/acrylic/mica to W11 ASAP! Maybe they will...by copying Apple this time.
    • iPad, Phone and Watch just updated. Downloading this now for my MBA
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      272
    3. 3
      ATLien_0
      256
    4. 4
      Edouard
      203
    5. 5
      snowy owl
      191
  • Tell a friend

    Love Neowin? Tell a friend!