Nesting For Loops 11

Tell us what’s happening:
Describe your issue in detail here.
//i have solved this problem this way please review once and comment

  **Your code so far**

function multiplyAll(arr) {
// let product = 1;
// Only change code below this line
//   for (let i = 0; i < arr.length; i++) {
//   for (let j = 0; j < arr[i].length; j++) {
//     product*=arr[i][j]
//     console.log(arr[i][j]);
//   }
// }

let flatData=arr.flatMap((currentValue) =>currentValue)
let product=flatData.reduce((acc,mov)=>acc*mov,1)
return product

multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Nesting For Loops

Link to the challenge:

Cool, but can you follow the instructions and solve it with two for loops?

1 Like

Hi @md.farhan0203 !

I have added spoiler tags around your code this is a working solution.

@colinthornton yes i did that after getting answer i comment the loop bro …

yes its working fine code review purpose i posted that

  1. Why flatMap instead of flat?
  2. Why not chain the methods?
  3. Why the extra ()s for a single argument arrow function?
  4. Why initialize with 1 in your reduce instead of using the first element?
  5. What does mov mean?
  6. Why the inconsistent use of spaces?
1 Like

You can format your code by right clicking the editor and selecting “Format Document”, or use the listed keyboard shortcut. This makes all the spacing consistent, which in turn makes it easier to read. If you’re going to ask for people to review your code, you should make it easy to read.

I agree with everything Jeremy said, except for #3 because that’s the default in Prettier, and I just let Prettier do its thing (the supposed advantage is so that you don’t have to add the parentheses in the case that you need to add a second argument, but I’ve grown to like the consistency as well).

1 Like

That’s fair. So long as you have a reason

1 Like

1)its combo of flat and map we can use flatMap to make flat and make iterate it
2)ok function multiplyAll(arr) {

let flatData=arr.flatMap((currentValue) =>currentValue).reduce((acc,mov)=>acc*mov,1)

return flatData
3)for findind product start with 1 else all goes 0
4)to iterate it
5)current value

  1. But you aren’t actually mapping anything, only flattening. Since you are only flattening, you should only use flat, not flatMap.

  2. That doesn’t make sense. ‘to iterate it’ doesn’t say why you used a special first value when there is no need.

  3. My point by asking what mov means is that readers have no idea from that variable name. You should use descriptive variable names.

function multiplyAll(arr) {
  return arr
    .reduce((product, element) => product * element);

@JeremyLT thank you so much …i got it…all my mistakes…

1 Like

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