+ 13

can anyone explain these simple codes to me?

var age = "15"; var message = (age < " year")? true: (age > "year")? false : true; alert(message); correct answer: true. Why? let [learn, solo = "solo"] = "learn"; [solo, learn] = [learn, solo]; console.log(solo + learn); a) le b) learnsolo c) Error d) learn output: le. Why?

10th Jul 2019, 4:41 PM
Анна
7 ответов
+ 9
>>>PART 1 I want to provide an complementary explanation on the first quiz. First and foremost, the hungry alligator always eats the largest number. Have that in your mind! 1 oooo< eats the largest number 3 3 >oooo eats the largest number 1 Note: oooo< this is an alligator (=  Let's break down the question: var message = ("15" < " year") ? true: ("15" > "year") ? false: true; >Part A ("15" < " year") console.log("15" < " year"); //false empty strings convert to 0 in a comparison. This is the same as this: "15" < "0year" //false console.log("15" < "year"); //true We discover here that ("15" < " year") gets false. The ternary operator (condition ? truthy : falsy) returns the falsy condition: ("15" > "year").
11th Jul 2019, 10:24 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 10
;) In Javascript, strings are treated mostly as arrays. So, this is the same as: let [learn, solo = "solo"] = ["l", "e", "a", "r", "n"]; The string "solo" is an optional value for the variable solo; but it's overwritten by "e". So, learn = "l", solo = "e" Then, their values are switched, so solo + learn ("l" + "e") will be equal to "le"
10th Jul 2019, 5:54 PM
Airree
Airree - avatar
+ 8
>>>PART 2 > Part B ("15" > "year"). Just remember that letters are the big bosses who command over numbers when BOTH are STRINGS. console.log("bossLetters" > "10"); //true Different types (numbers vs strings) are always FALSE no matter the operator! console.log("bossLetters" > 10); //false console.log("bossLetters" < 10); //false Finally we discover that  this part ("15" > "year") ? false: true; The alligator is eating the number. Numbers are not great than strings/letters, right?. That's why the second falsy part returns and we finish our assignment to message variable to: true.
11th Jul 2019, 10:27 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 7
Airree , thanks for this and your other answers, it helps a lot
10th Jul 2019, 6:19 PM
Анна
+ 5
Mofey , thanks for your help!
10th Jul 2019, 6:14 PM
Анна