Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

function sum(arr, n) {
  // Only change code below this line

  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.2470 YaBrowser/23.11.0.2470 Yowser/2.5 Safari/537.36

Challenge Information:

Basic JavaScript - Replace Loops using Recursion

Who understands what is needed and how to do this in this task? Why is recursion needed? I don’t understand anything from the instructions, what it returns and how, then they say 1 then 0, it’s not possible to understand

1 Like

Some problems are easier to solve with recursion. That’s why it’s needed.

Can you be more specific about what exactly you don’t understand?

Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.

Do you know how to write a non-recursive solution here? Maybe with a loop?

1 Like

первых n элементов массива arr .
This is not clear to me, if I pass the values 5.8 in the function, then how can I understand the first n elements?

function multiply(arr, n) {
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[i];
console.log(product)
}
return product;

}
multiply(8,9)
I also don’t understand how this code works, in a loop we go through n from 9 to 1 in the range of values 8, it turns out that the result is 8[1,2,3,4,5,6,7,8,9] and how What is multiplied by product?

That code doesn’t make a sum. How would you write a loop that sums everything in ‘arr’?

I’m not sure you understood the lesson where you multiplied together all of the numbers in nested arrays?

On this site it is impossible to understand the examples and instructions, if I give examples and they can’t explain it to me, then it’s not the student’s fault, but here they can’t explain it so that the student understands the code,

Ok. Then try a different website.

With pleasure, but there I went through the examples, and here I now understand why there are answers, because the student will never understand your instructions.

Plenty of people do understand the instructions. I suspect that whatever translation tool you have between Russian and English isn’t very good for this sort of translation.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.