11 Replies - 524 Views - Last Post: 06 November 2013 - 01:13 AM

#1 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

How would I incorporate parseint() into this code?

Posted 05 November 2013 - 06:16 PM

How do I convert the value to an integer by using the parseint() function?
<!DOCTYPE HTML>
<html>
<head>
<title>BMI Calculator</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/* <![CDATA[ */
function calcBMI()
{
var  yourweight = document.bmi_result.weight.value;
var  yourheight = document.bmi_result.height.value;
document.bmi_result.BMI.value = yourweight * 703/(yourheight * yourheight);
}
/* ]]> */
</script>
</head>
<body>
<form action="post" name="bmi_result">
<P>Weight in Pounds =
<input type="text" name="weight" style="width: 50px" onchange="calcBMI()"/></p>
<p>Height in Inches =
<input type="text" name="height" style="width: 50px" onchange="calcBMI()"/></p>
<p>BMI Result =
<input type="text" name="BMI" style="width: 50px" onchange="calcBMI()"/></p>
<p><input type="button" value="<<<Click here to Calculate BMI>>>" onclick="calcBMI"/></p>
</form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: How would I incorporate parseint() into this code?

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3615
  • View blog
  • Posts: 12,441
  • Joined: 12-December 12

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 06:49 PM

Have you attempted to use it?

yourweight = parseInt(yourweight, 10);

https://developer.mo...bjects/parseInt
Was This Post Helpful? 0
  • +
  • -

#3 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 07:39 PM

I'll try that thanks!!

This post has been edited by Dormilich: 05 November 2013 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 08:56 PM

yeah and I cannot get it to work. I keep getting decimal numbers that never end.

This post has been edited by Dormilich: 05 November 2013 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 09:57 PM

View PostMescn, on 06 November 2013 - 04:56 AM, said:

I keep getting decimal numbers that never end.

thatís floating point arithmetics.

in JS there is only a single number type, 64bit double (IEEE 754). parseInt() reads in a string, interprets it as an Integer and returns a Double.

anyways, parseInt() is the wrong choice here. what if someone does not have a weight/height that is an Integer?
Was This Post Helpful? 0
  • +
  • -

#6 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 10:45 PM

I talked to som of my classmates and they told me to use parse(total) with the map of the result. so I used :

var  weight = document.form.weight.value;
var  height = document.form.height.value;
var total = weight * 703 / (height * height);
document.form.result.value = parseInt(total);



It worked. I get a whole number. Thank for all your help!! This forum is awesome (I am a new user) I just hate that I suck at this stuff I'm learning though.
Was This Post Helpful? 0
  • +
  • -

#7 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 10:54 PM

What do you mean if "someone does not have a weight/height that is an Integer?" I dont know what you mean by that. please explain. Its really hard for me to get this stuff.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 11:15 PM

Quote

What do you mean if "someone does not have a weight/height that is an Integer?"

since you didnít use parseInt() before the last post, something like a weight of 75.7 kg which would be reduced to 75 kg by parseInt().

Quote

var total = weight * 703 / (height * height);

that is already a number. if you want it rounded, use the toFixed() method of numbers.
Was This Post Helpful? 1
  • +
  • -

#9 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 11:39 PM

Okay thanks, certainly remember that for the future. Our class required that we use the "parseInt()" for some reason. I hope it was used well. I already turned in the assignment lets see how it falls out...Thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: How would I incorporate parseint() into this code?

Posted 05 November 2013 - 11:56 PM

usually parseInt() is used on input when you formally require an integer value (something like a count, where non-integers do not make sense), or where you need to parse non-decimal numbers (octal, hexadecimal, binary, etc.).

btw, as of late, Java≠Script can work with real integers, which is described in the Typed Array Specification. but thatís way beyond beginner level.
Was This Post Helpful? 0
  • +
  • -

#11 Mescn  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 02-November 13

Re: How would I incorporate parseint() into this code?

Posted 06 November 2013 - 12:21 AM

That is the assignment i am on now. Building Arrays. I bet I'll be on here pretty soon. I am going to read the chapter tonight and attempt it tomorrow for practice. have a good night.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,333
  • Joined: 08-June 10

Re: How would I incorporate parseint() into this code?

Posted 06 November 2013 - 01:13 AM

View PostMescn, on 06 November 2013 - 08:21 AM, said:

have a good night.

in 12 hours.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1