0

I am in a intro to programming class where the assignments are in visual logic. This week's class is a assignment to gather food intake, calorie intake and a if statement which I was able to do in logic. Next class is webdesign so i thought why not try the same thing in javascript and get some experience with javascript but i'm running into problems. My user input for the calories is not converting to a integer for me to add the values together. Help please, I've read and researched and tried multiple things. Code is below

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Calorie Burner Program</title>
<script type="text/javascript">

</script>

</head>
<body>
<script type="text/javascript">
//I named my variables here
    var TotalCal, Food1, Cal1, Food2, Cal2, Food3, Cal3, Food4, Cal4, Food5, Cal5, Food6, Cal6, Food7, Cal7, Food8, Cal8, Food9, Cal9, Food10, Cal10, Food11, Cal11,    Food12, Cal12, Food13, Cal13, Food14, Cal14, Food15, Cal15, Food16, Cal16, Food17, Cal17, Food18, Cal18, Food19, Cal19, Food20, Cal20
    //i'm prompting for the food
    Food1 = window.prompt("What did you eat today?");
    //Here i'm prompting for calories and I would like to reference the input above.  as you can see from the code i've tried multiple suggestions
    Cal1 = parseInt(prompt("How many calories was " + Food1, "0"), 10);
    Food2 = window.prompt("What did you eat today?");
    Cal2 = parseInt(prompt("How many calories was " + Food2, "0"), 10);
    Food3 = window.prompt("What did you eat today?");
    Cal3 = window.prompt("How many calories was" + " " + Food3);
        ....
    Food20 = window.prompt("What did you eat today?");
    Cal120 = window.prompt("How many calories was" + " " + Food20)
    //you can see below that i've tried multiple methods and i get 5 everytime because my calories is 5 on every item i enter for testing
    TotalCal = Cal1, + Cal2, + +Cal3, + +Cal4, + +Cal5, + +Cal6, + +Cal7, + +Cal8, + +Cal9, + +Cal10,+ +Cal11, + +Cal12, + +Cal13, + +Cal14, + +Cal15, + +Cal16, +  +Cal17, + +Cal18, + +Cal19
    alert("The number of calories you consumed today was " + TotalCal);


    </script>
</body>
</html>

2 Answers 2

1

You are looking for the parseInt() function.

var numStringOne = "1";
var numStringTwo = "2";

var numOne = parseInt(numStringOne);
var numTwo = parseInt(numStringTwo);

var sum = numOne + numTwo; 

> sum == 3 == true
Sign up to request clarification or add additional context in comments.

2 Comments

There are some quirks to parseInt that you should be aware of: parseInt('Infinity') // NaN, parseInt('420TURNUP') // 420 parseInt('100.991') // 100
@usandfriends there is also parseFloat() which should be used in the 3rd case
0

Use the plus sign to convert to a number.

var numStringOne = "1";
var numStringTwo = "2";

var numOne = +numStringOne;
var numTwo = +numStringTwo;
var sum = numOne + numTwo;
if (sum === 3) {
    console.log("Hooray!");
}

Here's some reading about that:

And notes about using parseInt(), as another person suggested:

3 Comments

perfect. I thought it would be the parseInt function but figured my syntax was incorrect. I just fixed it using both of your suggestions with the syntax and it works perfect.
now if i could figure out a for or while loop to do this so that i don't have to type out lines of code for 20 different items that would be great. That is my next learning challenge i think
For your other issue, you should look into making an array of objects using JSON (object notation), and you could iterate over that array of objects or collect data into that array of objects.

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.