Function Returns NaN - javascript

I am trying to find the distance between two points.
This is my distance function.
var distance = function(first, second) {
var midValue = first - second;
midValue *= midValue;
return Math.sqrt(midValue);
}
This is where I call the distance function.
xDist += distance(locations[i].x, students[j][0]);
When I look at the xDist's value i get NaN. I have implicitly looked the values of locations and students they are all numbers. Also, in the distance function I have looked the value of Math.sqrt(midValue) before I return it and it is a number.

My guess is that locations[i].x or/and students[j][0] is/are undefined because arithmetic operations on undefined or, maybe they are not numbers in the first place, and thus always output NaN

You need to assign a initial value of xDist=0 else it would be undefined.
undefined+10= NaN
you can try this
var distance = function(first, second) {
var midValue = first - second;
midValue *= midValue;
return Math.sqrt(midValue);
}
var xDist=0;
xDist += distance(locations[i].x, students[j][0]);
alert(xDist);

Though it seems number it might not be number always. So better do a parseInt.
var distance = function(first, second) {
var midValue = parseInt(first) - parseInt(second);
midValue *= midValue;
return Math.sqrt(midValue);
}

Related

Round to the next whole number javascript

I want to achieve something like this in JavaScript:
input = 2455.55
f(input) = 2456
f(input) = 2460
f(input) = 2500
f(input) = 3000
f(input) = 2455.55
I am using the Math.round() method for now but only get to 2,546 with it. Wondering if there is a best way to achieve the rest.
You can divide your number by ten until you get a non-integer, round it up and then multiply by ten again the same amount of time. Something like this:
function roundUp(n) {
var n2 = n;
var i=0;
while (Number.isInteger(n2)) {
n2 /= 10;
i++;
}
return Math.round(n2) * Math.pow(10, i);
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2456)); // 2460
console.log(roundUp(2460)); // 2500
console.log(roundUp(2500)); // 3000
Based on your desired output it looks like you need to track the number of function calls. This doesn't seem to be an argument to your function.
Given the constraint that you have only a single argument, the implementation looks probably like
var lastNum = 0
var digitsToRound = 0
function roundUp(input) {
// Verify whether the function is called with the same argument as last call.
// Note that we cannot compare floating point numbers.
// See https://dev.to/alldanielscott/how-to-compare-numbers-correctly-in-javascript-1l4i
if (Math.abs(input - lastNum) < Number.EPSILON) {
// If the number of digitsToRound exceeds the number of digits in the input we want
// to reset the number of digitsToRound. Otherwise we increase the digitsToRound.
if (digitsToRound > (Math.log10(input) - 1)) {
digitsToRound = 0;
} else {
digitsToRound = digitsToRound + 1;
}
} else {
// The function was called with a new input, we reset the digitsToRound
digitsToRound = 0;
lastNum = input;
}
// Compute the factor by which we need to divide and multiply to round the input
// as desired.
var factor = Math.max(1, Math.pow(10, digitsToRound));
return Math.ceil(input / factor) * factor;
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2455.55)); // 2460
console.log(roundUp(2455.55)); // 2500
console.log(roundUp(2455.55)); // 3000
thanks, nice one! Inspired by your answer, I solved it like so:
function roundNumber(num, n) {
const divider = Math.pow(10, n);
return Math.round(num / divider) * divider;
};

How to count digits of given number?

I want the user to enter a number and print back the amount of digits of that number.
I know that I can use length, but my homework asking for while loop.
This is what I have so far:
var num;
var count = 0;
num = prompt('Enter number: ');
function counter(x, y) {
while (x > 0) {
y++;
x /= 10;
}
return y;
}
var result = counter(num, count);
console.log(result);
When I give the number 3456 (example), I get back the number 328. I want it to print back the number 4.
This line:
x /= 10;
Should be changed to:
x = Math.floor(x / 10);
The logic assumes integer division: 1234 is supposed to become 123, 12, 1 and 0. JavaScript does not have built in integer division so you need to use Math.floor to emulate it. Complete example with some fixes:
function countDigits(num) {
var count = 0;
while (num > 0) {
num = Math.floor(num / 10);
count++;
}
return count;
}
var num;
do {
num = Number(prompt("Enter number:"));
} while (Number.isNaN(num));
num = Math.abs(num); // just in case you want to handle -ve numbers
var result = countDigits(num);
console.log(result);
The problem is that the division operation will eventually end up converting x to a float and you'll have something like:
x / 10 === 0.1;
x / 10 === 0.01;
x / 10 === 0.001;
....
if you always parse (round) the result of the division to an integer, you'll get the expected result.
var num;
var count = 0;
num = prompt('Enter number: ');
function counter(x, y) {
while (x > 0) {
y++;
x = parseInt(x / 10);
}
return y;
}
var result = counter(num, count);
console.log(result);
You could check againt a number by taking the power of a decimal count.
function counter(value) {
var decimals = 0;
do {
decimals++;
} while (value >= 10 ** decimals)
return decimals;
}
console.log(counter(0));
console.log(counter(1));
console.log(counter(7));
console.log(counter(42));
console.log(counter(999));
console.log(counter(1000));
console.log(counter(1001));
First of all you should convert the input into a number, preferably using the Number function (using unary + has the same effect).
Secondly a division like 5 / 10 will return 0.5 which is bigger than 0. You should instead check if the number is bigger than or equal to 1.
function counter(num) {
num = Math.abs(num) / 10;
var count = 1;
while (num >= 1) {
count++;
num /= 10;
}
return count;
}
console.log(counter(+prompt('Enter number: ')));
You could also use a do while loop and avoid having an extra division outside the loop.
As others have pointed out, y doesn't need to be a parameter, it can be a local variable. But that's not your problem; let's add some extra logging to your loop:
function counter(x) {
let y=0;
while (x > 0) {
console.log("x=" + x + ", y=" + y);
y++;
x /= 10;
}
return y;
}
counter(3456);
The output looks like this:
x=3456, y=0
x=345.6, y=1
x=34.56, y=2
x=3.4560000000000004, y=3
x=0.3456, y=4
x=0.03456, y=5
...
You wanted the loop to stop at 0.3456, but that's still more than 0. (This mistake actually gives you a chance to learn something extra: can you explain why the loop ever finishes at all?)
Hopefully this will give you enough of a hint to complete the homework assignment - remember that debugging is an extremely important part of programming.
Please don't use cycles to measure length of an integer...
Use math instead! Logarithm will do much better job for you.
function numberLength(number) {
return Math.floor(Math.log10(Math.abs(number))) + 1
}
console.log(numberLength(YOUR_NUMBER));
This code returns NaN when the input is 0. I think it depends on your philosophy what length the 0 should have, so I am leaving that case unhandled.

How find square root n by computing the next Xi term on javascript

Write the function sqrt(A) for computing square root of positive real numbers using next numerical method xi+1 = (1/2) * (xi +(A/xi)). Where the A - input rial number;
On zero iteration next statements have been taken: x0 = A;
The error should be at least 10^-6
You could take the last value xi-1 and compare it with the new value xi instead of using a loop counter.
function sqrt(a, x = 1) { // take 1 for x(0) as start value for recursion
var y = (x + a / x) / 2; // prepare next value x(i+1)
if (x === y) { // exit condition
return x;
}
return sqrt(a, y); // tail call optimization
} // https://stackoverflow.com/q/310974/1447675
console.log(sqrt(2));
console.log(sqrt(10));
console.log(sqrt(9));
console.log(sqrt(25));
Looks like your requirement is not just finding the square root of a number. If by any chance that is your requirement, use Math.sqrt.
If your requirement is to implement a function to find the square root for educational purpose, what you need is to write a recursive function as below. Modify the code as required to support error at 10^-6
function sqrt(A, i = 0) {
if (i === 0)
return A;
let prev = sqrt(A, i - 1);
return 0.5 * (prev + (A / prev));
}
console.log(sqrt(2,1000));
console.log(sqrt(3,1000));
console.log(sqrt(9,1000));
console.log(sqrt(25,1000));

For comparing a calculated value to a fixed precision number, is string conversion better or mathematical calculation?

I have the following two scenarios where I want to compare a certain calculated value to float value 0.05.
In the first scenario, the value is being converted into String to get the value in two decimal place and then converting it back into a number for comparison.
var soneFunction = function(value)
{
var a = ((Math.round(value * 10) / 10) - value).toFixed(2);
if(Number(a) === 0.05)
a = -0.05;
return a;
};
In the second scenario, I am not doing any string-number conversion but using mathematical functions.
var soneFunction = function(value)
{
var roundingValue = (Math.round(value * 10) / 10) - value;
// fix the value till 2 decimal places
var a = Math.round((roundingValue) * 100) / 100;
if(a === 0.05)
a = -0.05;
return a;
};
I am curious to know which one is better?
UPDATE:
By better, I meant performance and memory consumption wise.
I think you can use
let parseFloatWithPrecision = function (value, precision){
var floatValue = parseFloat(value) || 0.0 ;
floatValue = floatValue.toFixed(precision);
return parseFloat(floatValue);
}
let compare = function(val){
if(parseFloatWithPrecision(val, 2) == 0.05)
return -0.05;
return val;
}

Fibonacci Sequence - Find the number of digits - JavaScript

So, I have successfully written the Fibonacci sequence to create an array with the sequence of numbers, but I need to know the length (how many digits) the 500th number has.
I've tried the below code, but its finding the length of the scientific notation (22 digits), not the proper 105 it should be returning.
Any ideas how to convert a scientific notation number into an actual integer?
var fiblength = function fiblength(nth) {
var temparr = [0,1];
for(var i = 2; i<=nth; i++){
var prev = temparr[temparr.length-2],
cur = temparr[temparr.length-1],
next = prev + cur;
temparr.push(next);
}
var final = temparr[temparr.length-1].toString().length;
console.log(temparr[temparr.length-1]);
return final;
};
a = fiblength(500);
console.log(a);
Why not use the simple procedure of dividing the number by 10 until the number is less than 1.
Something as simple as this should work (a recursive def obv works as well)
function getDigits(n) {
var digits = 0;
while(n >= 1) {
n/=10;
digits += 1;
}
return digits;
}
getDigits(200);//3
getDigits(3.2 * 10e20);//=>22
Here's a solution in constant time:
function fiblength(n) {
return Math.floor((n>1)?n*.2089+.65051:1);
}
Let's explain how I arrived to it.
All previous solutions will probably not work for N>300 unless you have a BigNumber library in place. Also they're pretty inneficient.
There is a formula to get any Fibonacci number, which uses PHI (golden ratio number), it's very simple:
F(n) = ABS((PHI^n)/sqrt(5))
Where PHI=1.61803399 (golden ratio, found all over the fibonacci sequence)
If you want to know how many digits a number has, you calculate the log base 10 and add 1 to that. Let's call that function D(n) = log10(n) + 1
So what you want fiblength to be is in just the following function
fiblength(n) = D(F(n)) // number of digits of a fibonacci number...
Let's work it out, so you see what the one liner code will be like once you use math.
Substitute F(n)
fiblength(n) = D(ABS((PHI^n)/sqrt(5)))
Now apply D(n) on that:
fiblength(n) = log10(ABS((PHI^n)/sqrt(5))) + 1
So, since log(a/b) = log(a) - log(b)
fiblength(n) = log10(ABS((PHI^n))) - log10(sqrt(5))) + 1
and since log(a^n) = n * log(a)
fiblength(n) = n*log10(PHI) - log10(sqrt(5))) + 1
Then we evaluate those logarithms since they're all on constants
and add the special cases of n=0 and n=1 to return 1
function fiblength(n) {
return Math.floor((n>1)?n*.2089+.65051:1);
}
Enjoy :)
fiblength(500) => 105 //no iterations necessary.
Most of the javascript implementations, internally use 64 bit numbers. So, if the number we are trying to represent is very big, it uses scientific notation to represent those numbers. So, there is no pure "javascript numbers" based solution for this. You may have to look for other BigNum libraries.
As far as your code is concerned, you want only the 500th number, so you don't have to store the entire array of numbers in memory, just previous and current numbers are enough.
function fiblength(nth) {
var previous = 0, current = 1, temp;
for(var i = 2; i<=nth; i++){
temp = current;
current = previous + current;
previous = temp;
}
return current;
};
My Final Solution
function fiblength(nth) {
var a = 0, b = 1, c;
for(var i=2;i<=nth;i++){
c=b;
b=a+b;
a=c;
}
return Math.floor(Math.log(b)/Math.log(10))+1;
}
console.log(fiblength(500));
Thanks for the help!!!
The problem is because the resulting number was converted into a string before any meaningful calculations could be made. Here's how it could have been solved in the original code:
var fiblength = function fiblength(nth) {
var temparr = [0,1];
for(var i = 2; i<=nth; i++){
var prev = temparr[temparr.length-2],
cur = temparr[temparr.length-1],
next = prev + cur;
temparr.push(next);
}
var x = temparr[temparr.length-1];
console.log(x);
var length = 1;
while (x > 1) {
length = length + 1;
x = x/10;
}
return length;
};
console.log ( fiblength(500) );

Categories

Resources