Converted Int returns NaN - javascript

Here is my code
var fifty = prompt("Enter amount ");
var twenty;
alert(twenty=parseInt(fifty/50) + " x " + "50 dollar bill");
alert(parseInt(twenty/20) + " x " + "20 dollar bill");
What I'm trying to count is dollar bills.
For example when I enter "120" it should return 2x 50 bills and 1x 20 dollar bill, I understand that returned value is String type so I'm converting them number, but on 20 dollar bills it returns "Nan x 20 dollar bils" I'm having trouble understanding why

Please stop using parseInt for separating integer part of float value, use it only to convert string into integer. When you are using parseInt float value is converted into string and than some integer number is parsed back, there can be situation when float number will be converted in string like 5.1234E-6 and parseInt will return 5 in this case. Use Math.floor() instead.
var sum = parseInt(prompt("Enter amount "));
var twenty = Math.floor(sum/50);
alert(twenty + " x " + "50 dollar bill");
alert(Math.floor((sum - (twenty*50))/20) + " x " + "20 dollar bill");

I've shuffled around your variable names to make it a bit clearer.
// I'll use the example of 120 to demonstrate
// The total amount, e.g. 120
var amount = prompt("Enter amount ");
// First, convert the inputted string into an int
var amountAsInt = parseInt(amount, 10);
// Then, divide by 50 (which equals 2.4), and then use
// Math.floor() to "chop off" the decimal part
var numberOfFifties = Math.floor(amountAsInt/50);
// Leaving us with numberOfFifties = 2
// We can now use 'modulus' to give the amount left.
// If you haven't come across modulus before, it gives
// the remainder after dividing by the given number (here: 50)
var amountLeft = amount % 50;
// Do the same with the amount left to find the number of 20s
var numberOfTwenties = Math.floor(amountLeft / 20);
if(numberOfFifties > 0){alert(numberOfFifties + " x " + "50 dollar bill(s)");}
if(numberOfTwenties > 0){alert(numberOfTwenties + " x " + "20 dollar bill(s)");}
JSFiddle here:
http://jsfiddle.net/7p57e3u1/3/
Reason for the NaN
You can see the reason why you were getting an NaN by looking at this JSFiddle.
http://jsfiddle.net/pto52oe5/
You are setting twenty to be equal to the whole string, hence it is "Not a Number" (NaN).
alert(twenty=parseInt(fifty/50) + " x " + "50 dollar bill");
Here, you appear to be assuming that
twenty=parseInt(fifty/50)
will be treated as a separate "part", but in actual fact, it uses the whole expression, setting twenty to be the whole string that is output in the alert():
twenty = parseInt(fifty / 50) + " x " + "50 dollar bill"
i.e. (for the example above)
twenty = "2 x 50 dollar bill"
A useful technique for debugging (and creating more understandable and therefore maintainable code) is to split things down into very simple steps, as I have in the example code above. This (arguably) is broken down too far, but use that as a first technique to break down a problem like this.

You have a problem in your logic.
Maybe something like that:
http://jsfiddle.net/2e7cs06v/
var total = prompt("Enter amount ");
var numOf50=parseInt(total/50);
var moneyLeft = total - (numOf50 * 50);
alert(numOf50 + " x " + "50 dollar bill");
alert(parseInt(moneyLeft/20) + " x " + "20 dollar bill");

Note that I'm not trying to fix the algorithm here, but just the input errors and type.
You should first parse fifty to an int, then only divide it by 50, since you want an int even after dividing by 50 you can let the initial parseInt.
Also, be aware that parseInt needs a radix (10 here), and it may still return NaN if a non parseable value is given.
var fifty = parseInt(prompt("Enter amount "), 10);
alert(twenty=parseInt(fifty/50, 10) + " x " + "50 dollar bill");
As an alternative, you could use the bitwise or (|) which acts almoste like parseInt(x, 10) except that when it would have returned NaN with parseInt, it would return 0 with the bitwise or operator.
Using |
var fifty = prompt("Enter amount ") | 0;
alert(twenty=((fifty/50)|0) + " x " + "50 dollar bill");

Use following :
var fifty = prompt("Enter amount ");
var twenty;
twenty=parseInt(fifty/50);
var rem = fifty - (twenty * 50);
alert(twenty + " x " + "50 dollar bill");
alert(parseInt(rem/20) + " x " + "20 dollar bill");
Hope it will help !!

Because you have parsing more string (+ " x " + "50 dollar bill") into parseInt() function. So parseInt function not able to pars "dollar bill" text value.
var fifty = prompt("Enter amount ");
var twenty = parseInt(fifty/50);
alert( twenty + " x " + "50 dollar bill");
alert(parseInt(twenty/20) + " x " + "20 dollar bill");

Related

JavaScript For Numbers

could you please tell me why is it used
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
Please share your best teaching cause I am new beginner
var input1 = prompt("Please type a starting number");
var bottomNumber = parseInt(input1);
var input = prompt("Please type a number");
var topNumber = parseInt(input);
var randomNumber = Math.floor(Math.random() * (topNumber - bottomNumber + 1)) + bottomNumber;
var message = "<p>" + randomNumber + " is a number between " + bottomNumber + " and " + topNumber + ".</p>";
document.write(message);
Let's unpack it from the inside out:
Math.random() gives a floating-point number between 0 and 1 (including 0, but not including 1). For example, 0.26254851654.
(topNumber - bottomNumber + 1) gives the number of different integers between bottomNumber and topNumber, including both of those themselves. Why + 1? Well, to find the number of integers between 5 and 10 for example: 5, 6, 7, 8, 9, 10. There are 10 - 5 + 1 = 6 of them, not 5. Let's call this numNumbers.
Multiply these two together, so we get a floating point number between 0 and numNumbers (still including 0 but not numNumbers itself).
We want an integer (whole number), so we round it down with Math.floor(). We now have an integer in the range 0, 1, ..., numNumbers - 1 (remember, the floating point number could get close to numNumbers, but never becomes equal, so numNumbers is not part of this range). Each of these is equally likely.
Add bottomNumber to end up with an integer in the range bottomNumber, bottomNumber + 1, ..., bottomNumber + numNumbers - 1. But what is bottomNumber + numNumbers - 1? It's exactly topNumber! So we now have an integer in the range bottomNumber, ..., topNumber, exactly what we needed.
Seems that randomNumber builds a random number between to numbers (a range). Being topNumber the max and bottomNumber the min in the range. For clarity:
const max = 10;
const min = 0;
const example = Math.floor(Math.random() * (max - min + 1)) + min;
this is overcomplicated for a beginner (sorry if you not a beginner) the Math.random() method returns a random number between 0 and 1 but if you want a random number between 0 and a specific number you have to use
Math.floor(Math.random() * x + 1) and "x" is the specific number

why javascript show string all arithmetic operation?

as you show below, when javascript doing an arithmetic operation all value concatenation with the string it shows a string value but I have some confusion...
var x = 10;
var y = 20;
var sum = x + y;
console.log("sum is :" + sum); //this is number
But confusion is
var x = 10;
var y = 20;
console.log("sum is : " + 10 + 20 ); //why this is string
var x = 10;
var y = "The value is " + x; // why this is string
var x = 10;
var y = 20;
var sum = x + y;
var z = 'sum is' + sum; //why this string
console.log("sum is : " + sum) // why this is not string coz it is also concatenation with string.
JavaScript will concatenate and coerce in a certain order of operations. You can add parentheses to add numbers before coercing to a string.
console.log("sum is : " + 10 + 20); // sum is : 1020
console.log("sum is : " + (10 + 20)); // sum is : 30
The unary + operator can be used to convert a variable to a number:
var y = "5"; // y is a string
var x = + y; // x is a number
If the variable cannot be converted, it will still become a number, but with the value NaN (Not a Number):
var y = "John"; // y is a string
var x = + y; // x is a number (NaN)
When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.
5 + null // returns 5 because null is converted to 0
"5" + null // returns "5null" because null is converted to "null"
"5" + 2 // returns "52" because 2 is converted to "2"
"5" - 2 // returns 3 because "5" is converted to 5
"5" * "2" // returns 10 because "5" and "2" are converted to 5 and 2
So if you put numbers inside parenthesis like (10 + 20) then it will perform arithmetic operation first then it will do the concatenation outside. If either one of them would be string then it would do the concatenation inside as well.
var console.log("sum is : " + (10 + 20) ); // sum is : 30
var console.log("sum is : " + (10 + '20') ); // sum is : 1020
When you are adding a number with a string it counts the number as a string, like console.log("sum is : " + 10 + 20 ).
But when 10 and 20 is under a variable it counts the number as a variable value.
If you want to use numbers with a string use "sum is: " + parseInt(10) like this.

why the odd results adding floats in javascript?

I have javascript code like this:
var strikePrice = parseFloat(this.props.data.strike).toFixed(1);
var commission = parseFloat(this.props.commission / 100).toFixed(2);
var callInMoney = parseFloat(strikePrice + this.state.callPrice + commission).toFixed(2);
var putInMoney = parseFloat(strikePrice - this.state.putPrice - commission).toFixed(2);
console.log("strikePrice: " + strikePrice + " commission: " + commission);
console.log("callprice: " + this.state.callPrice + " putprice: " + this.state.putPrice);
console.log("call: " + callInMoney + " put: " + putInMoney);
and the output is this:
strikePrice: 34.0 commission: 0.08
callprice: 0 putprice: 0
call: 34.00 put: 33.92
That is wrong. The call should be 34.08 (8 cents higher) just like the put is 8 cents lower.
Why is the results not correct?
Thank you
Matt
toFixed returns a string so you're actually doing some string concatenation rather than the arithmetic you expect.
Check out what happens when you just print out your initial addition.
var strikePrice = parseFloat('34').toFixed(1);
var commission = parseFloat('0.08').toFixed(2);
console.log(strikePrice + 0 + commission);
Instead, you'll need to convert those strings to numbers first.
var strikePrice = parseFloat('34').toFixed(1);
var commission = parseFloat('0.08').toFixed(2);
strikePrice = parseFloat(strikePrice);
commission = parseFloat(commission);
console.log(strikePrice + 0 + commission);
This expression:
strikePrice + this.state.callPrice + commission
Evaluates to this value:
"34.000.08"
Because commission is a string value, which is because [toFixed()][1] takes an integer and returns a string.
You need to refactor your code so that commission is a float value, or so that you call parseFloat() again on the parameters of line 3.
I can't comment on why putInMoney works for you. For me it gave "NaN".

Math.abs() Limit the amount of deimals

I have scoured the internet and I haven't found a solution that really works for me, yet.
var tv = Length * Type;
if (tv < 0)
{
cForm.voltage.value = "-" + Math.abs(tv) + " V";
}
else...
Some of the calculations with these two numbers come out to about the 15th decimal for some reason. I would like to limit the decimal amount that is returned, and NOT allow the number to round up or down. On a calculator it only comes out to about the third decimal, but Math.abs() brings it too far out.
.toFixed() Doesn't work for me because if the number only has 2 decimals it will add additional zeros at the end. I only want to display up to the fourth if it is calculated.
Just expanding on #goto-0 s comment, with the correct # of decimal places.
var tv = Length * Type;
if (tv < 0)
{
cForm.voltage.value = "-" + (Math.round(Math.abs(tv) * 10000) / 10000) + " V";
}
else...
Here's the implementation as a function that truncates the extra decimal places. If you want to round the output you could just use Number.toPrecision().
function toFixedDecimals(num, maxDecimals) {
var multiplier = Math.pow(10, maxDecimals);
return Math.floor(num * multiplier) / multiplier
}
console.log(toFixedDecimals(0.123456789, 4));
console.log(toFixedDecimals(100, 4));
console.log(toFixedDecimals(100.12, 4));
I'm sure its not the most efficient approach but it is pretty brainless -
grab your result
split it into an array based on the decimal point
then trim the decimal part to two digits (or however many you would like).
concat the pieces back together
Sorry for the long variable names - just trying to make it clear what was happening : )
// your starting number - can be whatever you'd like
var number = 145.3928523;
// convert number to string
var number_in_string_form = String(number);
// split the number in an array based on the decimal point
var result = number_in_string_form.split(".");
// this is just to show you what values you end up where in the array
var digit = result[0];
var decimal = result[1];
// trim the decimal lenght to whatever you would like
// starting at the index 0 , take the next 2 characters
decimal = decimal.substr(0, 2);
// concat the digit with the decimal - dont forget the decimal point!
var finished_value = Number(digit + "." + decimal);
In this case the finished_value would = 145.39

Why I got NaN in this javaScript code?

First I test that every variable got a number value:
09-11 18:15:00.420:
d_drop: -1.178791867393647
drop_at_zero: 0.0731037475605623
sightHeight: 4.5
d_distance: 40
zeroRange: 10
09-11 18:15:00.420:
d_drop: true
drop_at_zero: true
sightHeight: true
d_distance: true
zeroRange: true
function isNumber (o) {
return ! isNaN (o-0) && o != null;
}
var d_drop; // in calculation this gets value 1.1789
var d_path = -d_drop - sightHeight + (drop_at_zero + sightHeight) * d_distance / zeroRange;
console.log("Path: " + d_path + " cm");
and in the log:
09-11 18:15:00.430: D/CordovaLog(1533): Path: NaN cm
WHY? I have tried to figure that out couple of hours now and no success, maybe someone has an idea, I haven't!
Thanks!
Sami
-------ANSWER IS that parse every variable when using + operand-----------
var d_path = parseFloat(-d_drop) - parseFloat(sightHeight) + (parseFloat(drop_at_zero) + parseFloat(sightHeight)) * parseFloat(d_distance) / parseFloat(zeroRange);
The addition operator + will cast things as strings if either operand is a string. You need to parse ALL of your inputs (d_drop, sightHeight, etc) as numbers before working with them.
Here's a demo of how the + overload works. Notice how the subtraction operator - is not overloaded and will always cast the operands to numbers:
var numberA = 1;
var numberB = 2;
var stringA = '3';
var stringB = '4';
numberA + numberB // 3 (number)
numberA - numberB // -1 (number)
stringA + stringB // "34" (string)
stringA - stringB // -1 (number)
numberA + stringB // "14" (string)
numberA - stringB // -3 (number)
http://jsfiddle.net/jbabey/abwhd/
At least one of your numbers is a string. sightHeight is the most likely culprit, as it would concatenate with drop_at_zero to produce a "number" with two decimal points - such a "number" is not a number, hence NaN.
Solution: use parseFloat(varname) to convert to numbers.
If you're using -d_drop as a variable name, that is probably the culprit. Variables must start with a letter.
var d_drop = -1.178791867393647,
drop_at_zero = 0.0731037475605623,
sightHeight = 4.5,
d_distance = 40,
zeroRange = 10;
var d_path = d_drop - sightHeight + (drop_at_zero + sightHeight) * d_distance / zeroRange;
console.log("Path: " + d_path + " cm"); // outputs: Path: 12.613623122848603 cm

Categories

Resources