I have made a index.js file . And here is my code for that: function binarySearch (arr, value) {
var startIndex = 0;
var lastIndex = arr.length-1;
var middle = Math.floor((startIndex + lastIndex)/2);
while (arr[middle] != value && startIndex <= lastIndex) {
if (arr[middle] > value) {
lastIndex = middle - 1;
} else if (arr[middle] < value) {
startIndex = middle + 1;
}
middle = Math.floor((startIndex + lastIndex)/2);
}
return (arr[middle] != value ? -1 : middle);
}
Hi, I know that this is relatively old, but thanks for the helpful challenges!
I'm trying to solve the "flatten" challenge and I don't understand what I'm doing wrong. I checked my code in playcode.io and in jsbin and I get the expected [1,2,3,4,5] array, but the test you wrote says it gets [1,2,3,4]. This is my code:
function unpack(arr) {
if(!hasInternalArr(arr)) return arr;
var popped = arr.pop();
if(hasInternalArr(popped)) {
return arr.concat(unpack(popped));
}
else {
return arr.concat(popped)
}
}
function hasInternalArr(arr) {
if(!Array.isArray(arr)) return false;
return arr.some(function(curr) {
return Array.isArray(curr);
});
}
function flatten(arr) {
var toRet = [];
arr.map(function(curr) {
var unpacked = unpack(curr)
toRet = toRet.concat(unpacked);
});
return toRet;
}
Thanks!