# A question of scope and a loop

**URL:** <https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537>\
**Category:** JavaScript Bootcamp\
**Created:** [January 19, 2022, 3:44pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537 "2022-01-19T15:44:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunshine](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sunshine](https://group.classcentral.com/u/Sunshine)\
**Post date:** [January 19, 2022, 3:44pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537/1 "2022-01-19T15:44:37Z")

</div>

In the code below I start with a multidimensional array (arr [i][j]) and try to produce (1) the product of all numbers in the array and (2) the array (productArr) of all entries’ product in one dimension. If I place console.log(productArr) (line 14) inside the “for-loop” for j parameter, console.log(productArr) displays the productArr array which, of cause, is updating with each step. However, if I place console.log(productArr) at the end of function (line 19) I get no outcome. What is going on?

let productArr=[];  
let productArrTemp = 1;

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++){  
productArrTemp= productArrTemp\*arr[i][j];  
product \*=arr[i][j];  
}  
productArr.push(productArrTemp);  
console.log(productArr); // works fine  
productArrTemp = 1;  
}  
// Only change code above this line  
return product;  
console.log(productArr); // does not work!  
}

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

---

<div class="post-metadata">

**Author:** ![carlost2672543](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/carlost2672543/32/4162_2.png) [@carlost2672543](https://group.classcentral.com/u/carlost2672543)\
**Post date:** [January 19, 2022, 4:20pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537/2 "2022-01-19T16:20:15Z")

</div>

Hi Sunshine  
Are you sure is not working? In mine Browsers console I got 5040.

> [@Sunshine](#):
>
> let productArr=;  
> let productArrTemp = 1;
> 
> 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++){  
> productArrTemp= productArrTemp\*arr[i][j];  
> product \*=arr[i][j];  
> }  
> productArr.push(productArrTemp);  
> console.log(productArr); // works fine  
> productArrTemp = 1;  
> }  
> // Only change code above this line  
> return product;  
> console.log(productArr); // does not work!  
> }
> 
> multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

---

<div class="post-metadata">

**Author:** ![rivea](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/rivea/32/4378_2.png) [@rivea](https://group.classcentral.com/u/rivea)\
**Post date:** [January 19, 2022, 4:21pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537/3 "2022-01-19T16:21:45Z")

</div>

Just before `console.log(productArr)`, you `return product`. The function returns on that line, so the rest of the code is a dead code, therefore does not run. When you return from a function, whatever after that line will not run. Hope this helps.

---

<div class="post-metadata">

**Author:** ![Sunshine](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Sunshine](https://group.classcentral.com/u/Sunshine)\
**Post date:** [January 19, 2022, 4:37pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537/4 "2022-01-19T16:37:47Z")

</div>

the issue resolved. Many thanks, Eda

---

<div class="post-metadata">

**Author:** ![rivea](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/rivea/32/4378_2.png) [@rivea](https://group.classcentral.com/u/rivea)\
**Post date:** [January 19, 2022, 4:44pm UTC](https://group.classcentral.com/t/a-question-of-scope-and-a-loop/22537/5 "2022-01-19T16:44:06Z")

</div>

You’re very welcome. I’m glad it helped!
