/    Sign up×
Community /Pin to ProfileBookmark

Numbers keep showing up as “NaN”

I’m writing a program where a user can type in their weight, height, and age in order to find out how many calories they should eat if they want to maintain their weight. There’s one major issue though. Whenever I type in all the necessary information, the numbers appear as “NaN”. Here’s the code I wrote:

[code]<html>
<head>
<title>ITE154 BMR Calculator</title>

<style type=”text/css”>

body {
background-image: url(“http://newton.ncc.edu/gansonj/ite154/img/veggies_bkg.jpg”);
color: black;
font-family: comic sans ms;
}

#pagewrap {
border: 6px blue solid;
padding: 30px;
background-color: white;
width: 700px;
margin: 20px auto 0px auto;
border: 6px #81C6DD solid;
border-radius: 25px;
}

#contentwrap {
background: #99CCCC;
border: 6px #9E3B33 solid;
border-radius: 25px;
}

#header {
margin: 20px 20px 20px 20px;
text-align: center;
color: #ffffff;
background: #003399;
font-weight: bold;
font-size: 2em;
border-radius: 25px;
padding: 10px 0px 10px 0px;

}

#formwrap {
margin: 20px 20px 20px 20px;
height: 450px;
background: #CCCCCC;
border: 5px solid #9E3B33;
border-radius: 25px;
padding: 10px;
background-image: url(“http://newton.ncc.edu/gansonj/ite154/img/squash.png”);
background-repeat: no-repeat;
background-position: 375px 50px;
}

.formtext {
color: black;
margin-top: 8px;
}

.formfield {
padding: 5px;
background: #99CCCC;
color: black;
border: 2px #003399 solid;
border-radius: 25px;
font-weight: bold;

}

#calcbutton {
background: #E48743;
color: white;
cursor: pointer;
padding: 5px 0px 5px 0px;
border: 3px solid #90CA77;
width: 150px;
}

#calcbutton:hover {
background: #90CA77;
color: #000000;
border: 3px solid #E48743;
}

#footer {
margin: 20px 20px 20px 20px;
text-align: center;
color: white;
background: #003399;
font-weight: bold;
font-size: 1.45em;
border-radius: 25px;
padding: 20px 0px 20px 0px;
}

#output {
color: #003399;
}

</style>

<script src=”https://code.jquery.com/jquery-3.4.1.min.js” integrity=”sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=” crossorigin=”anonymous”></script>
<script src=”https://code.jquery.com/ui/1.12.1/jquery-ui.min.js” integrity=”sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=” crossorigin=”anonymous”></script>

<script type=”text/javascript”>

$(document).ready( function() {
$(“#calcbutton”).on( “click”, function(){
var weight = parseFloat( $(“#weight”).val() );
var height = parseFloat( $(“#height”).val() );
var age = parseFloat( $(“#age”).val() );
var fbmi = 655 + (4.3 * weight) + (4.7 * height) – (4.7 * age);
var mbmi = 66 + (6.3 * weight) + (12.9 * height) – (6.8 * age);
var level = $(“activity”).val();
var fcalories = fbmi * percentage;
var mcalories = mbmi * percentage;

if (level = “level1”){
var perecntage = .20;
}
else if (level = “level2”){
var percentage = .37;
}
else if (level = “level3”){
var percentage = .55;
}
else{
var percentage = .70;
}

var msg = “<div>To maintain your current weight…</div>”;
msg += “<div>If you are female, you must eat” + fcalories + “calories</div>”;
msg += “<div>If you are male, you must eat” + mcalories + “calories</div>”;
msg += “<div>More calories a day will result in weight gain</div>”;
msg += “<div>Less calories a day will result in weight loss</div>”;

$(“#output”).html( msg );
});
} ); // ends document.ready

</script>

</head>
<body>

<div id=”pagewrap”>

<div id=”contentwrap”>

<div id=”header”>
The Nifty ITE154 Calorie Counter
</div>

<div id=”formwrap”>

<form>
<div class=”formtext”>Enter your weight (pounds)</div>
<input type=”text” class=”formfield” id=”weight” size=”40″ value=”150″ />

<div class=”formtext”>Enter your height (inches)</div>
<input type=”text” class=”formfield” id=”height” size=”40″ value=”66″ />

<div class=”formtext”>Enter your age (years)</div>
<input type=”text” class=”formfield” id=”age” size=”40″ value=”44″ />

<div class=”formtext”>Select your activity level (1-4)</div>
<select id=”activity” class=”formfield”>
<option value=”level1″>Level 1 – Sedentary – little or no exercise</option>
<option value=”level2″>Level 2 – Lightly Active – exercise 1-3 times/week</option>
<option value=”level3″>Level 3 – Moderately Active – exercise 3-5 times/week</option>
<option value=”level4″>Level 4 – Very Active – exercise 6-7 times/week</option>
</select>

<div style=”margin-top:20px;”>
<input type=”button” value=”Calculate Calories” id=”calcbutton” />
</div>
</form>

<div id=”output”></div>

</div> <!– ends div#formwrap –>

<div id=”footer”>
Thanks for visiting!
</div>

</div> <!– ends div#contentwrap –>

</div> <!– ends div#pagewrap –>

</body>
</html>
[code]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 09.2021 — When checking for equality you need **two** equality signs:

`if(level == "level1"){`
Copy linkTweet thisAlerts:
@JIMBOYKELLYauthorMar 09.2021 — I just tried this, and the numbers still aren't showing.
Copy linkTweet thisAlerts:
@rpg2009Mar 10.2021 — @JIMBOYKELLY#1629093

var level = $("activity").val();

to this possibly. It's an id.

var level = $("#activity").val();
Copy linkTweet thisAlerts:
@JIMBOYKELLYauthorMar 10.2021 — I actually caught this error right after I posted this, but even after fixing it, it's still not working.
Copy linkTweet thisAlerts:
@rpg2009Mar 10.2021 — This seems to be quite old code.

**vars** instead of **const** or **let**. Concatenation instead of using template strings

The form could have labels and name properties and maybe _submit_ instead of _click_. Save having to search the DOM for input values that you could access from the form's properties instead.

That aside.

Your first issue
``javascript<i>
</i>var fcalories = fbmi * percentage;
var mcalories = mbmi * percentage;<i>
</i>
`</CODE>
At this point in the code <STRONG>**percentage**</STRONG> has not yet been assigned a value. it is <EM>_undefined_</EM>. That is why you are getting <EM>_NaN_</EM>

A quick solution. You could drop the above two lines and instead go for
<CODE>
`<i>
</i>...
var percentage = 0;

if (level === "level1"){
percentage = .20; // typo corrected and var removed
}
... and so on<i>
</i>
`</CODE>
then just add the calculation in your output<br/>
e.g.
<CODE>
`<i>
</i>"&lt;div&gt;If you are female, you must eat" + (fbmi * percentage) + "calories&lt;/div&gt;";<i>
</i>
``
×

Success!

Help @JIMBOYKELLY spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...