最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript addition program displaying "NaN" error - Stack Overflow

programmeradmin2浏览0评论

My question is about this simple program always displaying a "NaN" error.

My code for this simple function will always alert a "NaN" error for a simple addition instruction and I'm not sure how to resolve this.

The program should simply add miles and the bonus, which is 1000 if futureTrips is any value greater than 0.

My code is:

    var miles;
    var futureTrips;
    var totalMiles;
    var bonus;
    miles = prompt("Please enter your current miles");
    futureTrips = prompt("pleae enter amount of future trips");

    if (futureTrips > 0){
        (bonus = 1000);}
    else{
        (bonus = 0);}

   totalMiles = (bonus.value + miles.value);
   alert(totalMiles);

This will always display a "NaN" error. Any tips on how to fix this issue?

My question is about this simple program always displaying a "NaN" error.

My code for this simple function will always alert a "NaN" error for a simple addition instruction and I'm not sure how to resolve this.

The program should simply add miles and the bonus, which is 1000 if futureTrips is any value greater than 0.

My code is:

    var miles;
    var futureTrips;
    var totalMiles;
    var bonus;
    miles = prompt("Please enter your current miles");
    futureTrips = prompt("pleae enter amount of future trips");

    if (futureTrips > 0){
        (bonus = 1000);}
    else{
        (bonus = 0);}

   totalMiles = (bonus.value + miles.value);
   alert(totalMiles);

This will always display a "NaN" error. Any tips on how to fix this issue?

Share Improve this question edited Dec 15, 2017 at 9:21 yivi 47.7k18 gold badges130 silver badges155 bronze badges asked Dec 10, 2017 at 1:24 BCarusoBCaruso 532 silver badges5 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Problem:

bonus and miles are numbers (miles in fact is a string). You have to use them not their .value property that doesn't exist (thus is undefined), so:

totalMiles = (bonus.value + miles.value);

is the same as:

totalMiles = (undefined + undefined);

which is NaN.


Fix:

Use bonus and miles directly (you still have to convert miles to a number though):

totalMiles = bonus + Number(miles);

Working snippett:

var miles;
var futureTrips;
var totalMiles;
var bonus;

miles = prompt("Please enter your current miles");
futureTrips = prompt("pleae enter amount of future trips");

futureTrips = Number(futureTrips);

if (futureTrips > 0) {
  bonus = 1000;
} else {
  bonus = 0;
}

totalMiles = bonus + Number(miles);
alert(totalMiles);


Notes:

  1. prompt return strings, so you have to convert miles into a number first, otherwise you get a string concatenation instead of arithmitic addition. You don't have to convert bonus because it's already a number. You should also consider converting futureTrips to a number before the if.
  2. Parenthesis are not necessary around expressions: (bonus = 1000); is equivalent to bonus = 1000;.

In JavaScript, prompt returns string value. You can not apply .value on them. By default string values are converted to int before the addition with another integer (also known as coercing). But to be on the safe side you can use parseInt to convert string to int manually.

Change totalMiles = (bonus.value + miles.value); To

totalMiles = (bonus.value + parseInt(miles));

Try this:

var totalMiles;
var bonus;

var miles = parseInt(prompt("Please enter your current miles"),10);
var futureTrips = parseInt(prompt("pleae enter amount of future trips"),10);

if (futureTrips > 0) {
  bonus = 1000;
} else {
  bonus = 0;
}

totalMiles = bonus + miles;
alert(totalMiles);

I always use the radix in parseInt like paarseInt(miles,10). Or use parseFloat(miles).

Also don't use bonus.value or miles.value, just use miles since miles is the return string from then prompt mand and bonus is a number.

miles is a string here. To get the value as an integer I suggest instead of miles.value do parseInt(miles)

Your code modified:

    var miles;
    var futureTrips;
    var totalMiles;
    var bonus;

    do{
        miles =  prompt("Please enter your current miles") ; //assigns a string value to variable
        futureTrips = prompt("pleae enter amount of future trips");

        miles = parseInt(miles);                //convert string to number
        futureTrips = parseInt(futureTrips);
    }while( isNaN(miles + futureTrips) );       //optional loop, repeat prompts if NaN detected 

    if (futureTrips > 0){
        (bonus = 1000);}
    else{
        (bonus = 0);}

   totalMiles = (bonus + miles);                // removed bonus.value, did you mean to use the Object.ValueOf() method?
   alert(totalMiles);
发布评论

评论列表(0)

  1. 暂无评论