Here are the some of the important most asked Node.js programs related to Array for the interview, or in exam.
Node.js Important Array Programs
#1 Make a function that will take an array with number as the first argument and another number as a second argument and it should return all numbers below the second argument number.
Source Code:
function Arr(arr, ele){
for(let i=0; i<=arr.length; i++){
if(arr[i] == ele){
for(let j=i; j<=arr.length; i++){
if(i<=4){
console.log(arr[i])
}
else{
break;
}
}
}
}
}
Arr([1, 2, 3, 4, 5],3);
Output:
3
4
5
#2 Make a function that will take an array of any data type as first argument and second argument will be a variable to find in the array. If not found, it should return -1 otherwise it should return the index.
Source Code:
function checkArray(arr, ele){
for(let i=0; i<=arr.length; i++){
if(arr[i] == ele){
return i;
}
}
return -1;
}
//console.log(checkArray([1, 2, 3, 4, 5],3)); // output = 2
console.log(checkArray([1, 2, 3, 4, 5],6)); // output = -1
Output:
-1
#3 Make a function that will take an array with numbers as an argument and return another array with all the numbers doubled.
Source Code:
function double (arr){
let newArr = [];
for(let i = 0; i < arr.length; i++){
newArr.push(arr[i] * 2);
}
console.log(newArr) ;
}
double([3,4,5]);
Output:
[ 6, 8, 10 ]