+ 1

Tell me, how this code to write shorter?

let c = ['twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ', 'hundred'] function digital(numberThree) { for (let k = 1; k <= c.length + 100; k++) { k = numberThree; if (numberThree == k) { k -= 20; console.log(c[k]) break; } } } digital(20) // if will digital(30), that will be console.log(c[k - 9]), if will digital(40), that will be console.log(c[k - 18]), if will digital(50), that will be console.log(c[k - 27])

8th May 2021, 2:25 PM
Тимур Завьялов
Тимур Завьялов - avatar
2 odpowiedzi
+ 1
I don't know what you're trying to accomplish but your loop will only do a maximum of 1 iteration because your k variable is assigned a value and your condition to break the loop is hit immediately. The following will behave exactly like your digital function for-loop and be much shorter: let c = ['twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ', 'hundred'] function digital(numberThree) { console.log(c[numberThree - 20]) } These comments don't match your digital function implementation at all: // if will digital(30), that will be console.log(c[k - 9]), if will digital(40), that will be console.log(c[k - 18]), if will digital(50), that will be console.log(c[k - 27]) If instead, you just want digital(20) to print "twenty", digital(30) to print "thirty"..., this would work: let c = ['twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ', 'hundred'] function digital(num) { console.log(c[(num - 20) / 10]); }
8th May 2021, 3:03 PM
Josh Greig
Josh Greig - avatar
0
Josh, thanks)
8th May 2021, 3:09 PM
Тимур Завьялов
Тимур Завьялов - avatar