+ 9

Java challenge question explanations

Forum for explanation of answers for Java challenge questions.

31st Jan 2017, 5:29 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
3 Respostas
+ 14
thank you
5th Mar 2017, 3:20 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 9
What is the output of this code? 1 var arr = [1] //declare an array with 1 element 2 for (var i = 1; i < 3; i++) { //a "for" loop containing 1 and 2 3 arr [i] = arr [i-1] + i; //what? I don't understand this line 4 } 5 document.write(arr.join("")); //concatenation, needs explanation answer is 1 3 i think If someone could explain what is happening here I would be grateful, thank you
31st Jan 2017, 6:20 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 2
I think your code fails in the for (2)(3) By accessing array[1] you're trying to access something that doesn't exist (arr has only 1 element, arr[0] is the only index you can access)
3rd Mar 2017, 10:38 PM
Qwerty