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

function - Javascript calculator addition is not working properly - Stack Overflow

programmeradmin4浏览0评论

Here is my really basic Javascript calculator code. Except for the addition symbol, my calculator is just working fine. However, addition is not doing its job: instead it bines them. How to fix it?

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(first + second);
}
else if ( islem == "-") {
    document.write(first - second);
}
else if ( islem == "*" ) {
    document.write(first * second);
}
else {
    document.write(first / second);
}

Here is my really basic Javascript calculator code. Except for the addition symbol, my calculator is just working fine. However, addition is not doing its job: instead it bines them. How to fix it?

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(first + second);
}
else if ( islem == "-") {
    document.write(first - second);
}
else if ( islem == "*" ) {
    document.write(first * second);
}
else {
    document.write(first / second);
}
Share Improve this question edited Apr 19, 2017 at 13:18 Mihai Alexandru-Ionut 48.5k14 gold badges105 silver badges132 bronze badges asked Jan 29, 2017 at 20:14 MehmetMehmet 2271 gold badge4 silver badges12 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

The parseInt() function parses a string argument and returns an integer.

You have to assign parsed value to your variable, like this:

first=parseInt(first);
second=parseInt(second);

Or simply,

document.write(parseInt(first)+parseInt(second));

See reference here.

var first = prompt("First number");
var second = prompt("Second number");
parseInt(first);
parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(parseInt(first)+parseInt(second));
}
else if ( islem == "-") {
    document.write(first-second);
}
else if ( islem == "*" ) {
    document.write(first*second);
}
else {
    document.write(first/second);
}

When you use parseInt(), you need to capture the returned value so that you can use that number going forward. Right now, you are calling parseInt() and not doing anything with the returned value, so it is lost immediately.

It is also advisable to use the optional second parameter to parseInt(), which is the radix value that sets the base of the number you are working with. Typically, you'll want that to be 10 when working with base 10 numbers. This is really important because if the string you are working with starts with a 0 and you don't supply the radix, the operation will assume you are working with an octal value and if the string were to start with 0x, then the operation will assume you are working with a hex (base 16) value.

See more about using parseInt().

The code should be:

var first = prompt("First number");
var second = prompt("Second number");
first = parseInt(first, 10);
second = parseInt(second, 10);

You are not storing the parseInt result anywhere, and instead performing operations on strings. Since + is also a string concatenation operation, you get the concatenated string as a result. Try changing lines 3-4 to:

first = parseInt(first, 10); //don't forget the base!
second = parseInt(second, 10);
//TBD: add some error handling code to check values

Here is the right program:

var first = prompt("First number");
var second = prompt("Second number");
var firstParsedInteger = parseInt(first);
var secondParsedInteger = parseInt(second);
var islem = prompt("Is it +/-/* or /?");
if (islem == "+" ) {
    document.write(firstParsedInteger + secondParsedInteger);
}
else if ( islem == "-") {
    document.write(first-second);
}
else if ( islem == "*" ) {
    document.write(first*second);
}
else {
    document.write(first/second);
}

Note: You were never using the parsed value. In javascript, if you are using var for numbers and using '+' operator. this treats those numbers as string not as a number. Remember to parse integer when playing with var and + in javascript.

These answers are all great! I'd go one step further and

1) Call parseInt() on the prompts immediately, so that you're not declaring as many unnecessary variables

2) Use a switch statement instead of an if/else! It'll make it easier to add other cases, for exponentiation, etc. This one's more of a style choice, though :)

3) You can also consider using parseFloat() instead of parseInt() so that it can handle decimal inputs!

var first = parseInt(prompt("First number"));
var second = parseInt(prompt("Second number"));
var islem = prompt("Is it +/-/* or /?");
switch (islem) {
    case "+":
        document.write(first+second);
        break;
    case "-":
        document.write(first-second);
        break;
    case "*":
        document.write(first*second);
        break;
    case "/":
        document.write(first/second);
        break;
    default:
        document.write("Excuse me? Give me an operator, please!");
};
发布评论

评论列表(0)

  1. 暂无评论