References
How to convert string into float in javascript? jQuery calculation NaN
Goal
Take two numerical input values from the user. Divide the second number by the first number. Multiply the result by 100 so it is represented as a percentage. Add each number and the result to individual lists. Call the function getTextInput() via onClick command.
Background
I primarily work with Java; I am currently in a data structures class (second year CS student). My proficiency is intermediate. My familiarity with javascript is basic.
Problem
NaN results. I have tried multiple methods to achieve this task; I parsed both input values and assigned them to new variables, and I parsed both initial string inputs to a float value. However, I am still having a problem parsing the result of these two numbers of type int or string to a float value. Should I be parsing to a float value of parameters of type int? When I convert the string values to int values the program won’t accept the input values and crashes. Why is this happening? I have looked at several related posts, but cannot find anything that provides the information I am looking for. What is the best method to go about this? Any help or direction is greatly appreciated. Thanks.
NOTE Entire code posted at the very bottom.
Method 1 - parsed two strings to float. ‘NaN’ result when I printed percentOfNumbers and percentageList to the console.
var num1 = document.getElementById("numString1"); var num2 = document.getElementById("numString2");
percentOfNumbers = parseFloat(num2/num1 * 100); percentageList.push(percentOfNumbers); percentOfNumbers.value = "";
// add num1 to List1 List1.push(num1.value); num1.value = "";
// add num2 to List2 List2.push(num2.value); num2.value= "";
Method 2 - parsed each text input to an int value, parsed the result to a float value. ‘NaN’ result when I printed percentOfNumbers and percentageList to the console.
var num1 = document.getElementById("numString1"); var num2 = document.getElementById("numString2");
var num1Parsed = parseInt(num1); var num2Parsed = parseInt(num2);
percentOfNumbers = parseFloat(num2Parsed/num1Parsed * 100); percentageList.push(percentOfNumbers); percentOfNumbers.value = "";
// add num1 to List1 List1.push(num1Parsed.value); num1Parsed.value = "";
// add num2 to List2 List2.push(num2Parsed.value); num2Parsed.value= "";
// EXAMPLE INPUT VALUES, 2000 for num1, 2233 for num2
// Entire Code
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
--><!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
// separate lists for repeat user input values for num1, num2, and percentageOfNumbers
var List1 = [];
var List2 = [];
var percentageList = [];
var percentOfNumbers = 0;
var num1Parsed = 0;
var num2Parsed = 0;
function getTextInput() {
// retrieve two numbers of type 'String' from user and assign to appropriate var
var num1 = document.getElementById("numString1");
var num2 = document.getElementById("numString2");
// parse each string input number into an int
num1Parsed = parseInt(num1);
num2Parsed = parseInt(num2);
// divide second number by first number
// parse result and assign to percentOfNumbers var
// add percentOfNumbers value to percentageList
// reassign percentOfNumbers to ""
percentOfNumbers = (num2Parsed/num1Parsed * 100);
percentageList.push(percentOfNumbers);
percentOfNumbers.value = "";
// add num1 to List1
List1.push(num1Parsed.value);
num1Parsed.value = "";
// add num2 to List2
List2.push(num2Parsed.value);
num2Parsed.value= "";
}
</script>
</head>
<body>
<input id="numString1" type="text" name="input1" value="" />
<input id="numString2" type="text" name="input2" value="" />
<input id="inputField" type="button" name="search" value="compute" onClick="getTextInput();" />
</body>
</html>