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

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...