Hi guys,
Just read the solution to a problem in JavaScript and tell me if you understand the
comment.
can u understand the logic and problem from the comments?
Intermediate algorithms JavaScript challenge Dropit
function dropElements(arr, func) {
let index=null;
//loop through arr
for(var i=0; i<arr.length; i++){
let number=Number(arr[i]); //convert element to number
//pass array element to function
if(func(number)) //if the number statisfy the condition it return true or false
{
index =i; //find first index of element for which it is true
break; //first true element then break
}
}
//console.log(index);
if(index === null){ //if no element in arr statisfy the function return empty array
return [];
}
return arr.slice(index,arr.length); //return the array from the first element that statisfy the condition
}
console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));
console.log(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}));
console.log(dropElements([0, 1, 0, 1], function(n) {return n === 1;}));
console.log(dropElements([1, 2, 3], function(n) {return n > 0;}));
console.log(dropElements([1, 2, 3, 4], function(n) {return n > 5;}));
console.log(dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}));
console.log(dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}));
if i cant post full solution i will make edits!!
is my comments ok… can another programmer read and understand my solution?