Signup/Sign In

Evaluate Code Questions for Array.forEach Test

JavaScript Avg. Score: 0.25 883 Intermediate
Q.1 What will be the output of the following code:
const ratings = [5, 4, 5];
let sum = 0;

const sumFunction = async (a, b) => a + b;

ratings.forEach(async (rating) => {
  sum = await sumFunction(sum, rating);
});

console.log(sum);

 

Q.2 Answer the question based on the following code:
[2, 5, , 9].forEach(function(element, index) {
    console.log(`a[${index}] = ${element}`);
});

What will the function log for the 3rd element(element on the 2nd index) of the array?

Q.3 Answer the question based on the following specifications:

Given an array with the name arr, and a function processElements that is to be with each element of the array, what will be the correct way to use the forEach() function if you have to provide a reference to some object obj while processing each element of the array using the processElements function. The object is used inside the processElements function.

Q.4 How many times the callback function for the given Array.forEach() will get executed?
const arr = [1, 3, /* empty */, 7];

arr.forEach((element) => {
  console.log({ element });
});

 

Q.5 What will be the output of the following code:
const arr = [1,2,3,4,5];

let newArr = arr.forEach((element) => {
  return element+1;
});

console.log(newArr)

 


Studytonight logo

Find me on YouTube

I make videos for Fullstack Dev, AWS, Ethical Hacking and Foundational stuff.