+ 1

Can you subscribe this code for me and why the output of this code is 4?

Let arr=[1,3,4,2] Let count=0 for(let i=0; i<are.lenght; i++){ i==arr[i] count++ } console.log(arr)

14th Apr 2020, 12:41 PM
Arsham Aazami
Arsham Aazami - avatar
2 Answers
0
Arsham Azami hi, Is this the whole code and exactly how you wrote it? If so there are some syntaxes errors: *Don't capitalize --> let not Let. *are.lenght --> are is not defined,so arr and length not length. *i==arr[i] --> == is comparator , so it would return a Boolean value, don't you want to use = only to assign arr[i] to i. Once that fixed, I tried in playground and console.log(arr) output 1,3,4,2. From my point of view, the only way to have 4 is by console.log(arr[2]), or console.log(arr.length) or if you used i=arr[i] and console.log it inside the for loop it would make sense as on the first loop: i=0 --> arr[0]=1 --> i become 1 Then i++ so i=2 Then second loop: i=2 --> arr[2]=4 --> i become 4 Then i++ so i=5 5>arr.length so no third loop. Apart from that,if it isn't missing some code , I couldn't figure out why it would give 4.
14th Apr 2020, 4:17 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 2
Oh I should have written "=" instead of "=="😑 thank's for your help and effort to find answer
14th Apr 2020, 5:23 PM
Arsham Aazami
Arsham Aazami - avatar