+ 1

Javascript chellenges, what is output of this code?

I'm new with Javascript and I love Sololearn chellenges, but i have trouble with some questions in quiz. I'm sorry if this is not where I need to post this stuff but I need to find out few things. 1st question: What is the output of this code? var a=3, b=7; if((a++ > 5) && (b > 4)) document.write(b); else document.write(a); The answer is 4. Well, I see that correct is ''else'' but what I don't know why is 4 instead of 3, in if part saying a++ but how is that affect if correct is else statement? I thought the correct answer would be 3. 2nd question: What is output of this code? var a = [1,2,3,4,5]; var c = []; c[0] = new Array(a); a[c.length] = 3; alert(c[0]); Correct answer in this one says 1,3,3,4,5 and i wondered why, I need explanation with this. This is maybe funny questions to those who has more experience so please don't laugh. :)

23rd Oct 2017, 8:16 PM
KreÅ”imir Prgić
KreÅ”imir Prgić - avatar
4 Answers
+ 2
1st question: The answer is 4 because after comparing 'a'(which is currently 3) with 5, it is incremented by 1 using the '++' operator. 2nd question: var a is an array with [1,2,3,4,5]. Then var c is an empty array. Then c[0] refers to the first index of array 'c' which is assigned with the value of array 'a'. Which makes 'c' a multidimensional array --- c = [[1,2,3,4,5],undefined,undefined,...]. Then c.length refers to the length of array c which is 1. Then 'a[c.lenght] =3' is equivalent to 'a[1] = 3'. That replaces 2 with 3 in array 'a'. Thats why alert(c[0]) outputs 1,3,3,4,5. I hope you understand. Feel free to ask any concepts you don't understand.
23rd Oct 2017, 11:56 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 2
The answer should be 7. First, x is assigned with the value of 7. Then y is assigned with the value of x which is currently 7. Then x is incremented by 1 using the expression '++'. (x++ and ++x are evaluated differently). Then there is z which is equal to 'y modulo x'.(Ignore the ++ ) '%' is the modulo operator which basically gets the remainder of y/x. Which in this case is : y/x == 7/8 then y%x ==7. alert(z) == 7. I think the '++' operator confuses you. Basically it adds 1 to the variable. But it's execution depends on where it is placed(before or after the variable). In the quiz, it was 'y=x++' which assigns x to y before adding 1. That's why 'y' is 7 and x only becomes 8 after. If it was 'y=++x', 1 is added to x first then it is assigned to y. I hope you get my long explanation. PS: I'm not an expert. I'm still fairly new to JavaScript.šŸ˜ƒ
27th Oct 2017, 12:08 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 1
Thank you Jonathan for beautiful answer, I really love the way you explained. I hope you still getting notification on this one because I have more question I've gotten in quiz and I don't know how the correct answer is actually correct. var x = 7; var y = x++; var z = y++ % x; alert(z); It seems like a nice simple code, I also put the code in console and try to figured how this concept works, but for now it doesn't make any sense to me. I want you to know I really trying to understand every question before I post, but after hour of thinking I obviously need some expert opinion.
26th Oct 2017, 6:31 PM
KreÅ”imir Prgić
KreÅ”imir Prgić - avatar
+ 1
Again, very nice and detailed explanation. Now it make all sense, now I get it. Big respect and big thank you, this really means a lot to me. :)
27th Oct 2017, 12:55 PM
KreÅ”imir Prgić
KreÅ”imir Prgić - avatar