0

I am simply console logging the list of array with the variable temp I defined as showen below. but output only shows the third element on the array. what is this reason for not listing every element? because when I simply console log arr[i], it shows all 3 elements in the array but not with the vairable temp.

here is my code

function largestFour(arr){
 var max = [];
 for ( var i = 0; i < arr.length; i++)
 var temp = arr[i];
 console.log(temp);

}
largestFour([[4,5,1,3],[13,27,16,5],[10,19,4,9]]);


output 
(4)[10,19,4,9]
1
  • your temp variable is the last arr[i] which is the last element of your list (so the last array). I think that for what you want to, you are missing {} enclosing the logic in the for Commented Dec 29, 2018 at 1:43

5 Answers 5

2

You must define the {} block so the for know where it's the begining and end of the block execution.

function largestFour(arr) {
  var max = [];
  for (var i = 0; i < arr.length; i++) {
    var temp = arr[i];
    console.log(temp);
  }
}

largestFour([
  [4, 5, 1, 3],
  [13, 27, 16, 5],
  [10, 19, 4, 9]
]);

Edited

What actually happen here in your code

function largestFour(arr){
 var max = [];
 for ( var i = 0; i < arr.length; i++)
   var temp = arr[i];
 console.log(temp);

}

is the for loop when you don't define curly braces {} or a block execution, he just run ( and only ) the next line below the for loop.

Sign up to request clarification or add additional context in comments.

4 Comments

how can i do that?.
Now I did that for you, but when you type your answer, in the top bar you have a snippet button. An editor will popup and you can type your code there
@Yoarthur, I recommend you to get familiar with typescript because it's a modern trend. I've posted an answer with code written in typescript. It would be so funny and good to your career. :) You can try to execute my code in browser directly. Just copy/paste in browser console, which should work!
I've already using it on Angular and i can not wait to use it on Vue v3. The questions was made under the javascript tag, so the answer must base upon it?. Don't you think?. ;)
0

You miss the {}

Here is the code:

function largestFour(arr){
 var max = [];
 for ( var i = 0; i < arr.length; i++){
  var temp = arr[i];
  console.log(temp);
 }
}

Comments

0

I tried to refactor your code and it works

function largestFour(arr){
  for (var i = 0; i < arr.length; i++) {
    var temp = arr[i];
    console.log(temp);
  }
}
largestFour([[4,5,1,3], [13,27,16,5], [10,19,4,9]])

1 Comment

you did not refactor the code, you just added the {} because that was the only mistake :)
0

for ( var i = 0; i < arr.length; i++) is equivalent to for ( var i = 0; i < arr.length; i++); OR for ( var i = 0; i < arr.length; i++){}.

Also using var i to declare for loop initializer make them have the same scope as that of the loop itself i.e the function largetstFour().

So you can use braces {} and let instead of var for for loop initializor declaration.

function largestFour(arr){
  var max = [];
  for ( let i = 0; i < arr.length; i++)
  {
    var temp = arr[i];
    console.log(temp);
  }
}

Comments

0

The reason was that you didn't know context of if statement. if statement just wrap only a clause without curly brace {. So your code console.log was executed only once.

```

const largestFour = (arr) => {
    arr.forEach(item => console.log(item))
}

largestFour([[4, 5, 1, 3], [13, 27, 16, 5], [10, 19, 4, 9]])

```

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.