Last active
June 8, 2020 09:47
-
-
Save MarshySwamp/3770f9f201814669fa33e7c72b31a47f to your computer and use it in GitHub Desktop.
Code Snippet: Loop the input prompt until a number is entered and convert to an integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| // Loop the input prompt until a number is entered | |
| var origInput; | |
| while (isNaN(origInput = prompt("Enter a whole number:", "3"))); | |
| // Test if cancel returns null, then terminate the script | |
| if (origInput === null) { | |
| alert('Script cancelled!'); | |
| return | |
| } | |
| // Test if an empty string is returned, then terminate the script | |
| if (origInput === "") { | |
| alert('A value was not entered, script cancelled!'); | |
| return | |
| } | |
| // Convert decimal input to integer | |
| var inputToInteger = parseInt(origInput); | |
| // Now do something with the final prompt input... | |
| alert(inputToInteger); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment