0
var pagenumber = localStorage["pageno"];
var sum = pagenumber + i;
display(sum);

In this case i am getting the output sum = 11 (when input pagenumber = 1 and i = 1) , But i require the output sum = 2

7
  • 5
    You need to parse your string to a number. Commented Feb 1, 2018 at 17:02
  • 1
    var pagenumber = +localStorage["pageno"]; Commented Feb 1, 2018 at 17:03
  • localStorage["pageno"] returns an string '1' Commented Feb 1, 2018 at 17:04
  • Both values (or the first) aren't numbers but strings. 1 + 1 = 2 and '1' + '1' = '11'. Use parseInt(localStorage["pageno"]) to parse it. Commented Feb 1, 2018 at 17:04
  • Make sure you're are adding two numbers. Commented Feb 1, 2018 at 17:06

2 Answers 2

2

Need to parse the "1" in localStorage to an integer using the built in parseInt funtion:

var pagenumber = localStorage["pageno"];
var sum = Number.parseInt(pagenumber) + i;
display(sum);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use parseInt to convert your strings to integers in JavaScript:

var sum = parseInt(localStorage["pageno"]) + parseInt(i);
display(sum);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.