function - If...else statements to check for NaN and empty string not working in JavaScript - Stack Overflow

I refactored FizzBuzz using functions and everything is working great except for two if..else statement

I refactored FizzBuzz using functions and everything is working great except for two if..else statements in the function meant to validate the user's input (validateValue). I want to alert users that empty strings, decimals and NaN are not allowed. The statement checking for decimals works, but not empty strings or NaN. When I enter an empty string or NaN, the prompt for no decimals appears. I want to fix this using vanilla JavaScript, no jQuery.

Here is the JavaScript:

function getValue(message) {
  var msg = "Please enter a number from 1 to 100.";
  if (message) {
    msg = message;
  }
    return parseInt(prompt(msg)); 
}

function validateValue(num) { 
  if (num === "") { 
    return getValue("Please type something."); 
  } else if (num%1 !== 0) { 
    return getValue("No decimals allowed."); 
  } else if (isNaN(num)) { 
    return getValue("That is not a number!"); 
  } else {
    return num; 
  }
}

function fizzBuzz(num) {
   for (var i = 1; i <= num; i++) {
   if (i%15 === 0) {
    document.getElementById("list").innerHTML += "FizzBuzz<br>";
   }
   else if (i%3 === 0) {
    document.getElementById("list").innerHTML += "Fizz<br>";
  }
  else if (i%5 === 0) {
    document.getElementById("list").innerHTML += "Buzz<br>";
  }
  else {
    document.getElementById("list").innerHTML += i + "<br>";
  }
}
}

  var value = validateValue(getValue());
  fizzBuzz(value); 

Here is the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>FizzBuzz Refactor</title>
</head>
<body>
    <div>
    <p>Your FizzBuzz results:</p>
    <ul id="list">
    </ul>
    </div>
    <script type="text/javascript" src="app.js"></script>
</body>
</html>

I refactored FizzBuzz using functions and everything is working great except for two if..else statements in the function meant to validate the user's input (validateValue). I want to alert users that empty strings, decimals and NaN are not allowed. The statement checking for decimals works, but not empty strings or NaN. When I enter an empty string or NaN, the prompt for no decimals appears. I want to fix this using vanilla JavaScript, no jQuery.

Here is the JavaScript:

function getValue(message) {
  var msg = "Please enter a number from 1 to 100.";
  if (message) {
    msg = message;
  }
    return parseInt(prompt(msg)); 
}

function validateValue(num) { 
  if (num === "") { 
    return getValue("Please type something."); 
  } else if (num%1 !== 0) { 
    return getValue("No decimals allowed."); 
  } else if (isNaN(num)) { 
    return getValue("That is not a number!"); 
  } else {
    return num; 
  }
}

function fizzBuzz(num) {
   for (var i = 1; i <= num; i++) {
   if (i%15 === 0) {
    document.getElementById("list").innerHTML += "FizzBuzz<br>";
   }
   else if (i%3 === 0) {
    document.getElementById("list").innerHTML += "Fizz<br>";
  }
  else if (i%5 === 0) {
    document.getElementById("list").innerHTML += "Buzz<br>";
  }
  else {
    document.getElementById("list").innerHTML += i + "<br>";
  }
}
}

  var value = validateValue(getValue());
  fizzBuzz(value); 

Here is the HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>FizzBuzz Refactor</title>
</head>
<body>
    <div>
    <p>Your FizzBuzz results:</p>
    <ul id="list">
    </ul>
    </div>
    <script type="text/javascript" src="app.js"></script>
</body>
</html>
Share Improve this question edited Apr 3, 2016 at 16:27 user663031 asked Apr 3, 2016 at 16:23 ev662ev662 691 silver badge10 bronze badges 5
  • 3 Well, for starters, NaN % 1 yields NaN, and NaN !== 0 is true, so NaN % 1 !== 0 is also true. – Frédéric Hamidi Commented Apr 3, 2016 at 16:27
  • 1 num is something already processed by parseInt, so it cannot be "" – Aprillion Commented Apr 3, 2016 at 16:31
  • You never a get an empty string inside of validateValue, because you've already called parseInt – Bergi Commented Apr 3, 2016 at 16:31
  • as @charlietfl pointed out, you are checking for decimals after running parseInt, so "checking for decimals works" is not exactly true - decimals seem to be accepted without validation and truncated to integers – Aprillion Commented Apr 3, 2016 at 16:37
  • you don't validate the 2nd input when 1st is invalid - is that intentional or how many times do you want to prompt? – Aprillion Commented Apr 3, 2016 at 16:46
Add a ment  | 

3 Answers 3

Reset to default 3

You probably want something like:

function validateValue(num) {
  if (num === "") {
    return getValue("Please type something."); 
  }

  num = parseFloat(num);

  if (isNaN(num)) {
    return getValue("That is not a number!"); 
  } else if (num%1 !== 0) { 
    return getValue("No decimals allowed."); 
  } else {
    return num; 
  }
}

A string can't be a NaN. So you need to try to convert it to a number before you check if it's NaN.

You can try something like this:

function validate(str) {
  if (validateEmpty(str)) {
    console.log("Input is emply");
  } else if (validateNum(str)) {
    console.log("Value is numeric");
  } else {
    console.log("Invalid Input");
  }
}

function validateEmpty(str) {
  return str === undefined || str == null || str.toString().trim().length === 0;
}

function validateNum(str) {
  var reg = /[0-9]/i;
  return reg.test(str);
}

(function() {
  var a = "";
  validate(a);
  a = "123.46";
  validate(a);
  a = "123.46.57";
  validate(a);
  a = "absl123.46.57";
  validate(a);
  a = "test";
  validate(a);
})()

Thanks everyone! Here's my solution, mented out:

function getValue(message) {
  var msg = "Please enter a number from 1 to 100."; // default message
  if (message) {
    msg = message;
  }
    return prompt(msg); // return prompt with appropriate message based on value
}

function validateValue(num) {
  if (num === "") {
    return getValue("Please type something."); // check for empty string
  }

  num = parseFloat(num); // converts string to floating number

  if (isNaN(num)) {
    return getValue("That is not a number!"); // check for NaN
  } else if (num%1 !== 0) { 
    return getValue("No decimals allowed."); // check for decimals
  } else {
    return num; 
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745529533a4631628.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信