WHY THE FUNCTION IS RETURNING NaN - javascript

When I try to run the function below, the line of code is returning Nan, when it's supposed to return a number.
var next = list[last]+list[before];
This is the full code:
function fibonacciGenerator(n) {
var list = [0, 1];
if (n === 0) {
return [];
} else if (n === 1) {
return [0]
} else {
while (n > 1) {
var last = list.length;
var before = last - 1;
var next = list[last] + list[before];
list.push(next);
n -= 1;
return list;
}
}
}
console.log(fibonacciGenerator(8));

Because you're trying to access list[last], which is list[list.length]. However, because a list of length 1 has a single item at index 0, you're getting undefined. So, use list.length - 1:
function fibonacciGenerator(n) {
var list = [0, 1];
if (n === 0) {
return [];
} else if (n === 1) {
return [0]
} else {
while (n > 1) {
var last = list.length - 1;
var before = last - 1;
var next = list[last] + list[before];
list.push(next);
n -= 1;
return list;
}
}
}
console.log(fibonacciGenerator(8));
(Also, your function has a small issue - return list runs as soon as the first loop iteration has completed. Move it outside of the loop:
function fibonacciGenerator(n) {
var list = [0, 1];
if (n === 0) {
return [];
} else if (n === 1) {
return [0]
} else {
while (n > 1) {
var last = list.length - 1;
var before = last - 1;
var next = list[last] + list[before];
list.push(next);
n -= 1;
}
return list;
}
}
console.log(fibonacciGenerator(8));
Basically, the way it works in JavaScript is that array[array.length] will always be undefined. It starts at index 0 - so array.length is always one more than the index of the last element:
let nextElementIndex = array.length; // The next element to be added to the array will have the index of nextElementIndex, or array.length AT THIS CURRENT MOMENT
array.push(1); // `1` has the index nextElementIndex
nextElementIndex == array.length; // False, because there's a new element in the array, so the array's length has updated to nextElementIndex + 1.

It is because your last = 2, which makes list[last] = list[2] while it only has 2 elements that is list[0] and list[1]. Therefore, you get undefined out of this one, and when undefined + list[before], it gets NaN.

You just using index out of array range, an array has index range from 0 to length - 1, so array[length] + ... will return NaN
But, Something i can change in your code:
Try to think about calculating fibo(n) from fibo(n-1), not just recalculate from beginning.
Something like this look better:
var fibo = function(n){
// some funky code to check fatal input
....
// init special values
if(n==0) return [];
if(n==1) return [1];
if(n==2) return [1, 1];
// get previous state
var arr = fibo(n-1);
var len = arr.length;
arr.push(arr[len-2] + arr[len-1]);
return arr;
}
You will hear your computer sound like a helicopter if you must use this function many times. So if you need re-use this, you need something for caching calculated values. Ex:
epic_history = {
1: 1,
2: 1,
3: 2,
4: 3,
5: 5,
.....
}

Related

Javascript/ variable place/ what is wrong with the code

I want to write a Generator for Fibonacci numbers in Javascript;
0,1,1,2,5,7,12..... (to make the sequence you have to add the last two numbers)
But I have this problem when I assign the the output.length to a variable the code is not working, if I write it down straight instead of "newNumber" the code down is however working, but I don't understand what is wrong with the first one. Is it something wrong with the place of the variables?
function fibonacciGenerator(n) {
var output = [];
var lastNumber = output[output.length - 1];
var nPrev = output[output.length - 2];
var newNumber = lastNumber + nPrev;
if (n === 1) {
output = [0];
} else if (n === 2) {
output = [0, 1];
} else {
output = [0, 1];
for (var i = 2; i < n; i++) {
output.push(newNumber);
}
}
return output
}
console.log(fibonacciGenerator(5));
function fibonacciGen(n) {
const output = [0, 1];
// Return an empty array if n is less than 1
if (n < 1) {
return [];
}
// If n is 1 or 2, we can return the array now
if (n <2) {
return output;
}
// Loop through the remaining numbers in the sequence
for (let i = 2; i < n; i++) {
// Calculate the next number in the sequence
let lastNumber = output[output.length - 1];
let nPrev = output[output.length - 2];
let newNumber = lastNumber + nPrev;
// Add the new number to the output array
output.push(newNumber);
}
// Return the output array
return output;
}
console.log(fibonacciGen(5));
you have to declare your logic of next term in loop because first time length of output is zero
function fibonacciGenerator (n) {
var output =[];
if (n < 1) {
return [];
}
if (n===1){
output=[0];
}
else if (n===2){
output=[0,1];
}
else{
output=[0,1];
for( var i = 2; i < n; i++){
var lastNumber=output[output.length-1];
var nPrev=output[output.length-2];
var newNumber=lastNumber+nPrev;
output.push(newNumber);
}
}
return output
}

Longest decrease subsequence subarray in Javascript

I encountered this question:
Find longest decrease sequence in an array
So basically, if [3,1,4,3,2,1,2,3] is given, it will return [4,3,2,1] as the longest sequence within it. I was able to solve this question with O(n^2) time complexity but I feel like I can do O(n).
I know I am probably making a giant logic mistake but this is how I approach with 2 pointers method.
function findDecreaseSubArray(arr) {
let subList = [arr[0]];
// let maxLength= 0;
let left = 0;
for (let right = 1; right < arr.length; right++) {
// maxLength = (maxLength, windowEnd - windowStart + 1);
if (arr[left] > arr[right]) {
subList.push(arr[right]);
}else{
subList.shift();
left = right-1;
}
}
}
What I am trying to accomplish is moving left and right pointers if left element is larger than right, and if so, push both elements into sublist array.
My brain starts giving 404 error after this, so there are 2 subarrays that are decreasing, one of them is [3,1] and the other one is [4,3,2,1].
How could I track one subarray is larger than the other subarray? Or there is a better way to approach this solution?
Any hints, hands, code snippet would highly appreciated. (And sorry about my code snippet, I know it is shitty but I just wanted to display some of my thoughts on code)
You just have to iterate over the array once, but keep track of the start and length of the longest sequence as you progress through it:
var arr = [3,1,4,3,2,1,2,3];
function findDecreaseSubArray(arr) {
let startIndex = 0;
let length = 1;
let longestSequence = {
startIndex: 0,
length: 1
}
arr.forEach((element, index, arr) => {
if (index === 0) return;
if (element < arr[index -1]) {
length += 1;
} else {
length = 1;
startIndex = index;
}
if (length > longestSequence.length) {
longestSequence.length = length;
longestSequence.startIndex = startIndex;
}
})
return longestSequence;
}
console.log(findDecreaseSubArray(arr));
This approach supports VLAZ's comment and uses only the indices of the array.
function longestDecreasing(array) {
var longest,
start = 0,
i;
for (i = 0; i < array.length; i++) {
if (!i || array[i] < array[i - 1]) continue;
if (!longest || longest[1] - longest[0] < i - start) longest = [start, i];
start = i;
}
return array.slice(...longest && longest[1] - longest[0] > i - start
? longest
: [start, i]);
}
console.log(longestDecreasing([3, 1, 4, 3, 2, 1, 2, 3]))
It would probably be easier to iterate normally, from the first index to the end, while pushing sequential sequences to an array, which gets reset when a number greater than the last is found. When resetting, assign the resulting sequence array to a more permanent variable if it's longer than the array in that permanent variable:
const findDecreaseSubArray = (arr) => {
let longestSoFar = [];
let currentSequence = [];
const reset = (newItem) => {
if (currentSequence.length > longestSoFar.length) {
longestSoFar = currentSequence;
}
currentSequence = [newItem];
};
for (const item of arr) {
if (currentSequence.length && item > currentSequence[currentSequence.length - 1]) {
reset(item);
} else {
currentSequence.push(item);
}
}
reset();
return longestSoFar;
};
console.log(findDecreaseSubArray([3,1,4,3,2,1,2,3]));
Here is my code.
function getLongestDecreaseSequence(arr) {
if (
Object.prototype.toString.call(arr) !== "[object Array]" ||
arr.length == 0
) {
return arr;
}
let longestArr = [];
let tmpArr = [],
lastTmpIndex = -1;
arr.forEach((value, i) => {
if (arr[lastTmpIndex] < value) {
tmpArr = [];
}
// no matter what, I will put it in tmpArray
lastTmpIndex = i;
tmpArr.push(value);
if (longestArr.length < tmpArr.length) {
longestArr = tmpArr;
}
});
return longestArr;
}
console.log(getLongestDecreaseSequence([3, 1, 4, 3, 2, 1, 2, 3]));
This looks like a reducing job.
var a = [3,1,4,3,2,1,2,3],
r = a.reduce( function(r,e){
var l = r[r.length-1];
return l[l.length-1] > e ? ( l.push(e)
, r
)
: ( r.push([e])
, r
);
}
, [[]]
)
.reduce((p,c) => p.length > c.length ? p : c);
console.log(r);

how do i find the mode of an array of integers

it is a scenario where a use inputs an array of integers and it returns the most frequent integer.
it is tested 3 times but will fail once, whether its the 1st 2nd or 3rd test.
function arrayMode(array) {
if (array.length === 0) {
return null;
}
var sequence = {};
var maxNo = array[3],
maxCount = 3;
for (var i = 0; i < array.length; i++) {
var Entry = array[i];
if (sequence[Entry] === null)
sequence[Entry] = -1;
else
sequence[Entry]++;
if (sequence[Entry] > maxCount) {
maxNo = Entry;
maxCount = sequence[Entry];
} else if (sequence[Entry] == maxCount) {
maxNo += '&' + Entry;
maxCount = sequence[Entry - 1];
}
return maxNo;
}
}
console.log(arrayMode([1, 3, 3, 3, 1])) // output = 3
console.log(arrayMode([1, 2, 3, 1, 1])) // output = 1
console.log(arrayMode([2, 2, 2, 1])) // output = 2
I think there are several mistakes, if you find an entry not seen previously you set the sequence for that entry to -1, why not 1?
And you initialize maxNo to array[3] and maxCount to 3, why?
Does this make more sense to you?
function arrayMode(arr)
{
var mode = null;
var frequencies = [];
var maxFrequency = 0;
for (var i in arr)
{
var value = arr[i];
// if we haven't seen this value before, init its frequency to 0
if (frequencies[value]===undefined) frequencies[value] = 0;
// increase this value's frequency
frequencies[value]++;
// if this value's frequency is bigger than the max we've seen
// so far, this value is now the new mode.
if (frequencies[value] > maxFrequency)
{
maxFrequency = frequencies[value];
mode = value;
}
}
return mode;
}
If you want to return all modi in case there are more than one entries with the max number of frequencies (for example if for [1,1,2,3,3] you want the mode to be 1 & 3) then you can do so by a simple extra loop at the end.

Create a nested list from array

This is the 3rd question from Eloquent JavaScript chapter 4.
Basically it wants me to create a function that will make an array into a nested list from array.
E.g. arrayToList([1, 2, 3]) should return:
var list = {
value: 1,
rest: {
value: 2,
rest: {
value: 3,
rest: null
}
}
};
I wonder why my code leads to an infinite loop.
function arrayToList(arr) {
var list = {};
for (var i = 0; i < arr.length; i++) {
var a = arr[i];
function add(res) {
if (i == 0) {
res.value = a;
res.rest = "null";
}
else {
i -= 1;
add(res.rest);
}
}
add(list);
}
return list;
}
Thank you for taking a look!
You subtract 1 for i in the middle of the loop if it's not 0 - so it never finishes the for - you can use recursion for this!
function addToList(obj, arr, index) {
obj.value = arr[index];
if (index == (arr.length - 1)) {
obj.rest = null
} else {
obj.rest = {};
addToList(obj.rest, arr, index + 1)
}
}
var myObj = {};
var arr = [1,2,3];
addToList(myObj, arr, 0)
Demo: http://jsfiddle.net/freab275/
You are playing games with your loop counter and the recursion. Each time i is not 0 you subtract one, then the loop adds one. So with one step back and one step forward you never finish. Add some print statements to see what i is each time you reference it.

Find if Array Contains 5 consecutive Numbers with Javascript

I have a sorted Array that contains numbers. I want to be able to check if this Array(or similar Array), contains 5 numbers in consecutive order.
NOTE: Array may contain duplicate and double digit numbers.
I am trying this, but failing epically.
var array = [1,3,5,7,7,8,9,10,11]
var current = null;
var cnt = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] != current) {
if (cnt > 4) {
return true;
}
current = array[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 4) {
return true;
}
}
An iterative, straightforward approach would be:
var should_be_true = [1,3,5,7,7,8,9,10,11];
var should_be_false = [1,3,5,7,9,11,13,15,17];
var testArray = function(array) {
var conseq = 1;
for (var idx = 1; idx < array.length ; idx++) {
if (array[idx] == array[idx-1] + 1)
conseq++;
else
conseq = 1;
if (conseq == 5)
return true;
}
return false;
}
console.log(testArray(should_be_true)); //true
console.log(testArray(should_be_false)); //false
But for bonus fun, here's one variation on a functional approach, returning the position where the sequence starts, or -1 if no sufficiently long sequence exists:
should_be_true.map(function(curr,idx,arr) {
return (curr == arr[idx-1] +1) ? 1 : 0;
}).join('').search(/1{4}/);
A functional approach would be
function fiveInARow(array) {
// compare if one element is greater than or equal to the previous one
function compare(elt, i, arr) { return !i || elt >= arr[i-1]; });
// check if at a given position, every one of the last five comparisons is true
function check (_, i, greaters) {
return i >= 4 && greaters.slice(i-4, i) . every(Boolean);
}
return array . map(compare) . some(check);
}
The logic here is to first create an array of booleans using map, showing whether each element is greater than or equal to the previous. That yields an array such as [true, true, true, false, true].
The some part asks, for any element, is it the case that that element and every one of the preceding four elements are true? If so, it returns true.
Recursive solution
A recursive solution might be a bit easier to read.
function fiveInARow(array) {
return function _five(array, prev, n) {
if (n >= 5) return true;
if (!array.length) return false;
var next = array.shift();
return _five(array, next, next === prev ? n : next >= prev ? n+1 : 0);
}(array, -999999, 5);
}
var array = [1,3,5,7,7,8,9,10,11];
var cons=false;
var count=0;
for(i=0;i<array.length;i++){
if(array[i]+1==array[i+1]){
count++;
}
else{
count=0;
}
if(count==4){
cons=true;
}
}
if(cons){
//do what you want with it
}
DEMO
a more elegant way would be to define this whole thing in a function as below:
function checkCons(array){
var cons=false;
var count=0;
for(i=0;i<array.length;i++){
if(array[i]+1==array[i+1]){
count++;
}
else{
count=0;
}
if(count==4){
cons=true;
}
}
return cons;
}
and then using it like this:
var array = [1,3,5,7,7,8,9,10,11];
if(checkCons(array)){
//do what you want with it
}
DEMO
function fiveStraight() {
var array = [1, 3, 5, 7, 7, 8, 9, 10, 12];
var prev = array[0];
var numberOfStraight = 1;
for (var i = 1; i < array.length; i++) {
numberOfStraight = array[i] === prev + 1 ? numberOfStraight + 1 : 1;
prev = array[i];
if (numberOfStraight === 5) return true;
}
return false;
}
JSFIDDLE.
Here is what I found out to be the most straight forwards approach. Both descending and ascending values count as consecutive (if that's not your case, then fiddle with the Math.abs() call).
function consecutiveValuesCount(array) {
// sort the values if not pre-sorted
var sortedValues = array.sort();
// the result variable / how many consecutive numbers did we find?
// there's always atleast 1 consecutive digit ...
var count = 1;
for (var i = 0; i < sortedValues.length - 1; i++) {
// both descending and ascending orders count as we are using Math.abs()
if (Math.abs(sortedValues[i] - sortedValues[i+1]) == 1) {
++count;
}
}
return count;
}
//input
var array = [1,2,4,5,3];
// output
5
with this code you can find the highest number of consecutives for a given number or find the highest number of consecutives in general
var findMaxConsecutiveOnes = function (arr, number) {
//check for boundries
if(!number || !arr.length) return;
// maximum number of consectuives
let max = 0;
// count homy many consecutives before it ends (why it's 1 ? because a lonely number is still counted)
let counter = 1;
// you can ignore the next 2 variable if you want to use for loop instead of while
let length = arr.length;
let i = 1; // counting from index 1 because we are checking against index-1
while (i < length) {
if (arr[i] == arr[i - 1]) {
// boom, we have a consecutive, count it now
counter++;
} else {
// rest to 1
counter = 1;
}
// always update the max variable to the new max value
max = Math.max(counter, max);
//for the sake of iteration
i++;
}
return max== number;
};
console.log(findMaxConsecutiveOnes([5, 5, 5, 1, 1, 1, 1, 1]));
cnt will only increase once, when it hits the two 7s.
Put the incrementing line in the truthy condition, and the reset line in the else statement.
// Put into a function for testing.
function foo() {
var array = [1, 3, 5, 7, 7, 8, 9, 10, 11]
var current = null;
var cnt = 0;
for (var i = 0; i < array.length; i++) {
// Also need to make sure the next array item is a consecutive increase.
if (array[i] != current && array[i] === array[i-1] + 1) {
if (cnt > 4) {
return true;
}
current = array[i];
cnt++;
} else {
cnt = 1;
}
}
if (cnt > 4) {
return true;
} else {
return false;
}
};
// Call function.
alert(foo());

Categories