Implementation of JavaScript find() function


Took 4 minutes 42 seconds to code JavaScript find() method that returns all elements of the array meeting a specific condition. Here is the code snippet.

Code:
arr = [18, 10, 65, 70];
function find(arr) {
var arr2 = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > 18) {
arr2.push(arr[i]);
}
}
return arr2;
}

var new_arr = find(arr);
document.write(new_arr);

Result:
65,70

No comments:

Post a Comment

GITEX Dubai Talk by Saion Gupta and Saiasmi Gupta

Good morning, everyone. I'm Saion Gupta, the Founder of 10xCoderKids and the Guinness World Record holder for the youngest computer prog...