Skip to content

Instantly share code, notes, and snippets.

@MarshySwamp
Last active June 8, 2020 09:47
Show Gist options
  • Select an option

  • Save MarshySwamp/3770f9f201814669fa33e7c72b31a47f to your computer and use it in GitHub Desktop.

Select an option

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
(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