javascript unique random numbers - javascript

experts
I wish to generate unique random numbers between two numbers (from text box of web page).
I am using array to store numbers. when user click button it gives first random number, store it in array, and when user clicks button again, it generate random number, compares it with array numbers, and if different, stores it and displays it.
If maximum possible different numbers are show it, clears array and informs user.
I have written code, but it gives error : stack overflow, or sometimes I get duplicate results shown.
Can anyone shed a light on code:
var allnums = new Array();
var num1= new Number;
var num2= new Number;
function funClick()
{
var num1 = Number(document.getElementById('lnum').value);
var num2 = Number(document.getElementById('hnum').value);
if (allnums.length==num2)
{
alert("Maximum non-duplicate numbers served. Now resetting the counter.");
allnums = [];
return;
}
if (num1<num2)
{
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
funShow(x);
}
else
{
alert("You entered wrong number criteria!");
}
}
function funShow(x)
{
var bolFound = false;
for (var i=0;i<allnums.length;i++)
{
if((allnums[i])==x)
{
funClick();
}
}
if (bolFound == false)
{
document.getElementById('rgen').innerText = x;
allnums.push(x);
}
}

I fail to see how that code is generating a stack overflow (even though funShow has a call to funClick and funClick has a call to funShow, funShow's call to funClick should never happen because of a logic error -- fix the error and you'll get a stack overflow, though), but it has several issues. See the comments:
// style: Use [], not new Array()
var allnums = new Array();
// `new Number` doesn't do anything useful here
var num1 = new Number;
var num2 = new Number;
function funClick() {
// For user-entered values, use parseInt(value, 10) to parse them into numbers
var num1 = Number(document.getElementById('lnum').value);
var num2 = Number(document.getElementById('hnum').value);
if (allnums.length == num2) {
alert("Maximum non-duplicate numbers served. Now resetting the counter.");
allnums = [];
return;
}
// & is a bitwise AND operation, not a logical one. If your goal is to see
// if both numbers are !0, though, it works but is obtuse.
// Also, there is no ltnum2 variable anywhere, so trying to read its value
// like this should be throwing a ReferenceError.
if (num1 & ltnum2) {
// You're falling prey to The Horror of Implicit Globals, x has not
// been declared.
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
funShow(x);
} else {
alert("You entered wrong number criteria!");
}
}
function funShow(x) {
var bolFound = false;
// Again, & is a bitwise AND operation. This loop will never run, because
// you start with 0 and 0 & anything = 0
// But it should be throwing a ReferenceError, as there is no ltallnums
// anywhere.
for (var i = 0; i & ltallnums.length; i++) {
if ((allnums[i]) == x) {
funClick();
}
}
// This condition will always be true, as you've done nothing to change
// bolFound since you set it to false
if (bolFound == false) {
document.getElementById('rgen').innerText = x;
allnums.push(x);
}
}
There are two ways to approach this. Here's one that's basically what you were trying to do, but without recursion:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var targetCount;
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Find out how many integers there are in the range num1..num2 inclusive
targetCount = num2 - num1 + 1;
// Produce that many random numbers
while (nums.length < targetCount) {
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
if (nums.indexOf(x) < 0) {
nums.push(x);
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
The problem with that is that it can take a long time to fill the last few slots, since we have to hit them randomly.
The other way is to produce the array with the numbers in order, then mess it up. It can be dramatically more efficient for large ranges. Something like this:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (x = num1; x <= num2; ++x) {
nums.push(x);
}
// Sort it with a random comparison function
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
But, just doing the nums.sort(...) randomly once may well not be as successful at producing random results; see this article for more. (Thanks to eBusiness for that link and for his input on the below.)
So you may want to go further and throw in further random operations. Here's another example:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var n, x, y;
var num;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (n = num1; n <= num2; ++n) {
nums.push(n);
}
// We only need to shuffle it if it's more than one element long
if (nums.length > 1) {
// Sort it "randomly"
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Throw a bunch of random swaps in there
for (n = 0; n < nums.length; ++n) {
do {
x = Math.floor(Math.random() * nums.length);
}
while (x === n);
num = nums[x];
nums[x] = nums[n];
nums[n] = num;
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
That does the array sort thing as a starting point, but then does a bunch of random swaps between elements as well. It still runs in constant time, but should have a better result than using the array sort alone. Naturally, you'll want to test the distribution.

use an array:
var uniqueRandomNumbers = new Array();
var totalNumbers = 100;
for (var i=0; i<totalNumbers; i++){
uniqueRandomNumbers.push(i);
}
uniqueRandomNumbers.sort(function() {return 0.5 - Math.random();});
var uniqueNumber;
for(var i=0; i<uniqueRandomNumbers.length; i++){
uniqueNumber = uniqueRandomNumbers[i];
//do something with the number
}

Since I can't edit Crowder's answer, here is the simple unbiased way of scrambling an array:
function scramble(nums){
for (var n = nums.length; n; n--) {
var x = Math.floor(Math.random() * n);
var num = nums[n-1];
nums[n-1] = nums[x];
nums[x] = num;
}
}

Related

Number generation between 1 - 100 in JavaScript using JQuery [duplicate]

experts
I wish to generate unique random numbers between two numbers (from text box of web page).
I am using array to store numbers. when user click button it gives first random number, store it in array, and when user clicks button again, it generate random number, compares it with array numbers, and if different, stores it and displays it.
If maximum possible different numbers are show it, clears array and informs user.
I have written code, but it gives error : stack overflow, or sometimes I get duplicate results shown.
Can anyone shed a light on code:
var allnums = new Array();
var num1= new Number;
var num2= new Number;
function funClick()
{
var num1 = Number(document.getElementById('lnum').value);
var num2 = Number(document.getElementById('hnum').value);
if (allnums.length==num2)
{
alert("Maximum non-duplicate numbers served. Now resetting the counter.");
allnums = [];
return;
}
if (num1<num2)
{
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
funShow(x);
}
else
{
alert("You entered wrong number criteria!");
}
}
function funShow(x)
{
var bolFound = false;
for (var i=0;i<allnums.length;i++)
{
if((allnums[i])==x)
{
funClick();
}
}
if (bolFound == false)
{
document.getElementById('rgen').innerText = x;
allnums.push(x);
}
}
I fail to see how that code is generating a stack overflow (even though funShow has a call to funClick and funClick has a call to funShow, funShow's call to funClick should never happen because of a logic error -- fix the error and you'll get a stack overflow, though), but it has several issues. See the comments:
// style: Use [], not new Array()
var allnums = new Array();
// `new Number` doesn't do anything useful here
var num1 = new Number;
var num2 = new Number;
function funClick() {
// For user-entered values, use parseInt(value, 10) to parse them into numbers
var num1 = Number(document.getElementById('lnum').value);
var num2 = Number(document.getElementById('hnum').value);
if (allnums.length == num2) {
alert("Maximum non-duplicate numbers served. Now resetting the counter.");
allnums = [];
return;
}
// & is a bitwise AND operation, not a logical one. If your goal is to see
// if both numbers are !0, though, it works but is obtuse.
// Also, there is no ltnum2 variable anywhere, so trying to read its value
// like this should be throwing a ReferenceError.
if (num1 & ltnum2) {
// You're falling prey to The Horror of Implicit Globals, x has not
// been declared.
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
funShow(x);
} else {
alert("You entered wrong number criteria!");
}
}
function funShow(x) {
var bolFound = false;
// Again, & is a bitwise AND operation. This loop will never run, because
// you start with 0 and 0 & anything = 0
// But it should be throwing a ReferenceError, as there is no ltallnums
// anywhere.
for (var i = 0; i & ltallnums.length; i++) {
if ((allnums[i]) == x) {
funClick();
}
}
// This condition will always be true, as you've done nothing to change
// bolFound since you set it to false
if (bolFound == false) {
document.getElementById('rgen').innerText = x;
allnums.push(x);
}
}
There are two ways to approach this. Here's one that's basically what you were trying to do, but without recursion:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var targetCount;
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Find out how many integers there are in the range num1..num2 inclusive
targetCount = num2 - num1 + 1;
// Produce that many random numbers
while (nums.length < targetCount) {
x = Math.floor(Math.random() * (num2 - num1 + 1)) + num1;
if (nums.indexOf(x) < 0) {
nums.push(x);
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
The problem with that is that it can take a long time to fill the last few slots, since we have to hit them randomly.
The other way is to produce the array with the numbers in order, then mess it up. It can be dramatically more efficient for large ranges. Something like this:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var x;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (x = num1; x <= num2; ++x) {
nums.push(x);
}
// Sort it with a random comparison function
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
But, just doing the nums.sort(...) randomly once may well not be as successful at producing random results; see this article for more. (Thanks to eBusiness for that link and for his input on the below.)
So you may want to go further and throw in further random operations. Here's another example:
function funClick() {
var num1 = parseInt(document.getElementById('lnum').value, 10);
var num2 = parseInt(document.getElementById('hnum').value, 10);
var nums = [];
var n, x, y;
var num;
// Check the inputs
if (isNaN(num1) || isNaN(num2) || num2 <= num1) {
alert("Please ensure that hnum is higher than lnum and both are really numbers.");
return;
}
// Create an array with those numbers in order
for (n = num1; n <= num2; ++n) {
nums.push(n);
}
// We only need to shuffle it if it's more than one element long
if (nums.length > 1) {
// Sort it "randomly"
nums.sort(function(a, b) {
return 0.5 - Math.random();
});
// Throw a bunch of random swaps in there
for (n = 0; n < nums.length; ++n) {
do {
x = Math.floor(Math.random() * nums.length);
}
while (x === n);
num = nums[x];
nums[x] = nums[n];
nums[n] = num;
}
}
// Show the result
document.getElementById('rgen').innerText = nums.join(", ");
}
Live Example | Source
That does the array sort thing as a starting point, but then does a bunch of random swaps between elements as well. It still runs in constant time, but should have a better result than using the array sort alone. Naturally, you'll want to test the distribution.
use an array:
var uniqueRandomNumbers = new Array();
var totalNumbers = 100;
for (var i=0; i<totalNumbers; i++){
uniqueRandomNumbers.push(i);
}
uniqueRandomNumbers.sort(function() {return 0.5 - Math.random();});
var uniqueNumber;
for(var i=0; i<uniqueRandomNumbers.length; i++){
uniqueNumber = uniqueRandomNumbers[i];
//do something with the number
}
Since I can't edit Crowder's answer, here is the simple unbiased way of scrambling an array:
function scramble(nums){
for (var n = nums.length; n; n--) {
var x = Math.floor(Math.random() * n);
var num = nums[n-1];
nums[n-1] = nums[x];
nums[x] = num;
}
}

Optimal verification if a number is Palindrom in JS

I have a problem I am sitting on for the past few days.
I want to write an optimal (in JS) program for verifying if a number is a Palindrome.
My current approach:
function isPalidrom2(pali){
//MOST time consuming call - I am splitting the digit into char array.
var digits = (""+pali).split("");
//To get the length of it.
var size = digits.length;
var isPali = true;
for(var i = 0; i<Math.floor(size/2); i++){
//I am comparing digits (first vs last, second vs last-1, etc.) one by one, if ANY of the pars is not correct I am breaking the loop.
if(parseInt(digits[i]) != parseInt(digits[size-i-1])){
isPali = false;
break;
}
}
return isPali;
}
It's not optimal. The biggest amount of time I am waisting is the change from INT to STRING.
And I am out of ideas
- I tried to understand the BIT operators but I can't.
- I tried to google and look for alternative approaches - but I can't find anything.
- I tried to play with different algorithms - but this one is the fastest I was able to apply.
So in short - my question is:
"how can I make it faster?"
EDIT:
So the task I want to solve:
Find all of the prime numbers within the range of all five digit numbers.
Among all of the multiplies (i*j) they are between them, find the most significant palindrome.
My current approach:
function isPrime(number){
var prime = true;
var i
for(i = 2; i<= number/2; i++){
if((number%i)==0){
prime = false;
break;
}
}
return prime;
}
function get5DigitPrimeNr(){
var a5DigitsPrime = [];
var i;
for(i = 10000; i<100000; i++){
if(isPrime(i)){
a5DigitsPrime.push(i)
}
}
return a5DigitsPrime;
}
function isPalidrom(pali){
var digits = (""+pali).split("");
//we check if first and last are the same - if true, we can progress
size = digits.length;
return
(digits[0]==digits[size-1]) &&
(parseInt(digits.slice(1, Math.floor(size/2)).join("")) ==
parseInt(digits.reverse().slice(1, Math.floor(size/2)).join("")))
}
function isPalidrom2_48s(str) {
var str = str.toString();
const lower = str.substr(0, Math.floor(str.length / 2));
const upper = str.substr(Math.ceil(str.length / 2));
return lower.split("").reverse().join("") === upper;
}
function isPalidrom_22s(pali){
var digits = (""+pali).split("");
var size = digits.length;
for(var i = 0; i<Math.floor(size/2); i++){
//console.log("I am comparing: "+i+", and "+(size-i-1)+" elements in array")
//console.log("I am comparing digit: "+digits[i]+", and "+digits[(size-i-1)]+"")
if(digits[i] !== digits[size-i-1]){
//console.log('nie sa rowne, koniec')
return false;
}
}
return true;
}
function isPalidrom2_80s(pali){
return parseInt(pali) == parseInt((""+pali).split("").reverse().join(""))
}
function runme(){
var prime5digits = get5DigitPrimeNr();
var size = prime5digits.length;
var max = 0;
var message = "";
for(var i = 0; i<size; i++){
for(var j = 0; j<size; j++){
var nr = prime5digits[i]*prime5digits[j];
if(nr>max && isPalidrom2(nr)){
max = nr;
message = 'biggest palidrome nr: '+nr+', made from numbers: '+prime5digits[i]+' x '+prime5digits[j];
}
}
}
console.log(message)
}
function timeMe(){
var t0 = performance.now();
runme();
var t1 = performance.now();
console.log("Function took " + millisToMinutesAndSeconds(t1 - t0) + " s to find the perfect palindrom.")
}
//helper functons:
function millisToMinutesAndSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
To keep the spirit of you code, you could exit the loop with return instead of break and use the string directly without converting to an array. Strings have, as arrays, the possibility of an access single character with an index.
function isPalidrom2(value) {
var digits = value.toString(),
length = digits.length,
i, l;
for (i = 0, l = length >> 1; i < l; i++) {
if (digits[i] !== digits[length - i - 1]) {
return false;
}
}
return true;
}
console.log(isPalidrom2(1));
console.log(isPalidrom2(12));
console.log(isPalidrom2(1221));
console.log(isPalidrom2(123));
The fastest is probably to rely on javascripts native methods:
function isPalindrome(str) {
const lower = str.substr(0, Math.floor(str.length / 2));
const upper = str.substr(Math.ceil(str.length / 2));
return lower.split("").reverse().join("") === upper;
}
Or leave away all unneccessary conversions from your code:
function isPlaindrome(str) {
const half = str.length / 2;
for(var i = 0; i < half; i++)
if(str[i] !== str[str.length - i - 1])
return false;
return true;
}
If you're trying to speed things up, you could shave a few more seconds off by optimising your isPrime(n) function.
You don't need to check every factor, only the prime factors less than sqrt(n)
If you check every number from 2 to 99999 in ascending order, you can store the results as you go, so you don't need to keep re-calculating the list of previous primes
Something like this:
var savedPrimes = [2]
function isPrime(n){
// call this method with increasing values of n (starting at 2), saving primes as we go,
// so we can safely assume that savedPrimes contains all primes less than n
for(var i=0; i<savedPrimes.length; i++)
{
var f = savedPrimes[i];
if ((n % f) == 0)
return false; // found a factor
if (f*f>=n)
break; // stop checking after f >= sqrt(n)
}
// no prime factors - we found a new prime
savedPrimes.push(n);
return true;
}
function get5DigitPrimeNr(){
var a5DigitsPrime = [];
var i;
// first find all the primes less than 10000
for(i = 3; i<10000; i++){
isPrime(i);
}
// now find (and keep) the rest of the primes up to 99999
for(i = 10000; i<100000; i++){
if(isPrime(i)){
a5DigitsPrime.push(i)
}
}
return a5DigitsPrime;
}
EDIT - when I run your code with this method, I get a time of 10 sec
Code :
There are multiple methods that you can use (dunno if they are optimal) :
Palindrom = _ => (_=''+_) === [..._].sort(_=>1).join``
Some more :
let isPalindrome = __ => (_=(__=__+'').length)==0||_==1?!0:__[0]==__.slice(-1)?isPalindrome(__.slice(1,-1)):!1
let isPalindrome = (s,i) => (i=i||0)<0||i>=(s=''+s).length>>1||s[i]==s[s.length-1-i]&&isPalindrome(s,++i);
let isPalindrome = (str) => {
var len = ~~((str=str+'').length / 2);
for (var i = 0; i < len; i++)
if (str[i] !== str[str.length - i - 1])
return false;
return true;
}
Updated now takes numbers as input

My Solution to the Largest Palindrome of the product of two 3 digit numbers needs work

The answer I'm getting is not the correct one (correct answer is 906609). Please help me understand where I am going wrong. I want the while loop to go from 100 to 999 while multiplying itself against the current i value before the loop increments it.
// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.
var pali = [];
function palindrome() {
for (var i = 100; i <= 999; i++) {
var counter = 100;
while (counter <= 999) {
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join("")) {
pali.push(result);
}
counter++;
}
}
return pali[pali.length - 1];
}
console.log(palindrome());
You're going to have to sort the array in ascending order if you want the last one to be the highest:
pali.sort(function(a, b){return a-b});
Using that, I get 906609.
Lengthier, but faster. Starting at the max values and working down, short circuiting when we won't find higher values:
function largestPalindromeProduct(lower, upper) {
var palindrome = 0;
var outerLow = lower;
var outer = upper;
while (outer > outerLow) {
var inner = upper;
var innerLow = lower;
while (inner > innerLow) {
var result = inner * outer;
if (result + "" === (result + "").split("").reverse().join("")) {
if (result > palindrome) {
palindrome = result;
outerLow = inner; // Don't go lower than this value in the outer, no need!
}
inner = innerLow; // short-circuit this loop
}
inner--;
}
outer--;
}
return palindrome;
}
console.log(largestPalindromeProduct(100, 999))
I think the easiest way will be to add a If statement.
function palindrome()
{
var max = 0;
for (var i = 999; i >= 100; i--)
{
var counter = 999;
while (counter >= 100)
{
var result = counter * i;
if (result.toString() === result.toString().split("").reverse().join(""))
{
if(result>max)
{
max = result;
}
}
counter++;
}
}
return max;
}

Why does my code work with underscore.js but not when I use Ramda.js?

I am new to Javascript, I am doing a coding challenge to learn more about the language. This is not school related or anything like that, totally for my own personal growth. Here is the challenge:
Return the sum of all odd Fibonacci numbers up to and including the
passed number if it is a Fibonacci number.
I have spent the past 2 evenings working on solving this challenge. When I run my code using underscore.js it works. When I use Ramda.js it says NaN. I would think both would return NaN. I'm very surprised that I can get the correct answer from one and not the other. Any insights would be greatly appreciated!
var R = require('ramda');
function sumFibs(num) {
var fib_Arr = [];
var new_Arr = [];
var total = 0;
// I use this to tell if the fib num is greater than 2
var the_Bit = "false";
// This is used to keep track of when to stop the loop
var fib_Num = 0;
// THIS WORKS FROM HERE
// This loop generates a list of fibonacci numbers then pushes them to the fib_Arr
for(var i = 0; total < num; i++){
if (i < 1){
fib_Arr.push(0);
}
else if (i === 1){
fib_Arr.push(i);
fib_Arr.push(1);
}
else if (i === 2){
fib_Arr.push(2);
the_Bit = "true";
}
else if (the_Bit === "true"){
temp_Arr = R.last(fib_Arr,2);
temp_Arr = temp_Arr[0] + temp_Arr[1];
fib_Arr.push(temp_Arr);
total = R.last(fib_Arr);
}
// Generating the fib Array works TO HERE!!!!
}
// console.log(fib_Arr); // Print out the generated fibonacci array
// if last Array element is greater than the original in
var last_Element = R.last(fib_Arr);
if (last_Element > num){
console.log("The last element of the array is bigger!");
fib_Arr.splice(-1,1); // This removes the last item from the array if it is larger than the original num input
}
// This loop removes all of the EVEN fibonacci numbers and leaves all of the ODD numbers
for (var j = 0; j < fib_Arr.length; j++){
if (fib_Arr[j] % 2 !== 0){
new_Arr.push((fib_Arr[j]));
}
}
// This checks if the original input num was a
if (num % 2 !== 0){
new_Arr.push(num);
}
else{
console.log("The original num was not a Fibonacci number!");
}
// if last Array element is the same as the original input num
var last = R.last(fib_Arr);
if (last === num){
console.log("Removing the last element of the array!");
new_Arr.splice(-1,1); // This removes the last item from the array if it is the same as the original num input
}
// Now to add all of the numbers up :-)
for (var k = 0; k < new_Arr.length; k++){
console.log("This is fib_Num: " + fib_Num);
// console.log(fib_N`);
fib_Num = fib_Num += new_Arr[k];
}
return fib_Num;
}
// TEST CASES:
// console.log(sumFibs(75025)); //.to.equal(135721);
console.log(sumFibs(75024)); //.to.equal(60696);
You have a problem on these lines :
temp_Arr = R.last(fib_Arr,2);
temp_Arr = temp_Arr[0] + temp_Arr[1];
Besides the fact that R.last does not take a second argument (that will not fail though), you are using temp_arr as an array, when it is a number. Therefore, temp_arr gets a NaN value.
You are probably looking for R.take (combined with R.reverse) or R.slice.
By changing :
temp_Arr = R.last(fib_Arr,2);
with :
temp_Arr = R.take(2, R.reverse(fib_Arr));
or with :
temp_Arr = R.slice(fib_Arr.length - 2, fib_Arr.length)(fib_Arr);
or with (bonus play with a reduce from the right) :
temp_Arr = R.reduceRight(function(arr, elem) {
return arr.length < 2 ? [elem].concat(arr) : arr;
}, [])(fib_Arr);
We get :
sumFibs(75024) === 60696
For the record, here's how you do this problem:
function fibSumTo(n) {
var f1 = 1, f2 = 1, sum = 1, t;
while (f2 <= n) {
if (f2 & 1) sum += f2;
t = f1 + f2;
f1 = f2;
f2 = t;
}
return sum;
}
There's really no need for any sort of library because there's really no need for any sort of data structure.
var _ = require('underscore');function sumUpFibs (number){
arr_of_fibs = [1,1];
current = 1; //cursor for previous location
while (true){
var num = arr_of_fibs[current] + arr_of_fibs[current - 1];
if (num <= number) {
arr_of_fibs.push(num);
current++;
} else {
break;
}
}
console.log(arr_of_fibs);
var total = 0;
_.each(arr_of_fibs, function(fib){
total += fib;
})
return total;}console.log(sumUpFibs(75025));
This may be a better implementation... Though I know you're just starting so I don't want to come off as mean : D.... Also, maybe check your test cases too.

Javascript rounding issue when summing values [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
The user enters values in the first two text boxes and, as they type, Javascript (sorry, no jQuery, I'm not up to it yet) is used to calculate the precise sum and the sum rounded to 2 digits.
Why am I getting rounding error and what can I do to correct it?
Many thanks.
Hmmm....ParseFloat? Wrong data type?
What I would like to see if the precise answer as if it were added on a calculator. Is there a parseDecimal or other data type that I can use?
![enter image description here][1]
function SumValues() {
//debugger;
var txtSubsContrRbtAmt = document.getElementById("<%=txtSubsContrRbtAmt.ClientID%>");
var txtDeMinAmt = document.getElementById("<%=txtDeMinAmt.ClientID%>");
var txtTotRbtAmt = document.getElementById("<%=txtTotRbtAmt.ClientID%>");
var txtRndRbtAmt = document.getElementById("<%=txtRndRbtAmt.ClientID%>");
var total = Add(txtSubsContrRbtAmt.value, txtDeMinAmt.value);
txtTotRbtAmt.value = total;
txtRndRbtAmt.value = RoundToTwoDecimalPlaces(total);
}
function Add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
var currentValue;
if (isNumber(arguments[i])) {
currentValue = parseFloat(arguments[i]);
}
else {
currentValue = 0;
}
sum += currentValue;
}
return sum;
}
function RoundToTwoDecimalPlaces(input) {
return Math.round(input * 100) / 100
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
[1]: http://i.stack.imgur.com/5Otrm.png
Update. I am evaluating something like this:
function AddWithPrecision(a, b, precision) {
var x = Math.pow(10, precision || 2);
return (Math.round(a * x) + Math.round(b * x)) / x;
}
There is a golden rule for anyone writing software in the financial sector (or any software dealing with money): never use floats. Therefore most software dealing with money use only integers and represent decimal numbers as a data structure.
Here's one way of doing it:
(Note: this function adds two strings that looks like numbers)
(Additional note: No error checking is done to aid clarity. Also does not handle negative numbers)
function addNumberStrings (a,b) {
a = a.split('.');
b = b.split('.');
var a_decimal = a[1] || '0';
var b_decimal = b[1] || '0';
diff = a_decimal.length - b_decimal.length;
while (diff > 0) {
b_decimal += '0';
diff --;
}
while (diff < 0) {
a_decimal += '0';
diff ++;
}
var decimal_position = a_decimal.length;
a = a[0] + a_decimal;
b = b[0] + b_decimal;
var result = (parseInt(a,10)+parseInt(b,10)) + '';
if (result.length < decimal_position) {
for (var x=result.length;x<decimal_position;x++) {
result = '0'+result;
}
result = '0.'+result
}
else {
p = result.length-decimal_position;
result = result.substring(0,p)+'.'+result.substring(p);
}
return result;
}
*note: code is simplified, additional features left out as homework.
To fix your addition the way you want, I'd suggest counting the decimal places in each number somehow This method, for instance Then passing the max value to toFixed, and trimming any leftover zeroes.
function AddTwo(n1, n2) {
var n3 = parseFloat(n1) + parseFloat(n2);
var count1 = Decimals(n1, '.');
var count2 = Decimals(n2, '.');
var decimals = Math.max(count1, count2);
var result = n3.toFixed(decimals)
var resultDecimals = Decimals(result, '.');
if (resultDecimals > 0) {
return result.replace(/\.?0*$/,'');
}
else {
return result;
}
}
// Included for reference - I didn't write this
function Decimals(x, dec_sep)
{
var tmp=new String();
tmp=x;
if (tmp.indexOf(dec_sep)>-1)
return tmp.length-tmp.indexOf(dec_sep)-1;
else
return 0;
}
Here's a JSFiddle of that

Categories