0

Why does my loop only output one thing instead of looping?

My loop instead of looping only shows the end of the loop. The question is supposed to be an encryption which adds a $ between each word(ex: hi, hello, wassup becomes $hi$hello$wassup$). With my current code I only end up with the last word. I was considering testing a return at the end for the final $ at the end of everything but idk if that would work. Any help would be appreciated. Maybe a loop isn’t the best way to solve this. class Add { constructor(...words) { this.words = words; } //your code goes here print() { for (let i = 1; i <= this.words.length; i++ ) var blah = i - 1 console.log("

quot;+this.words[blah]) } //your code goes here } var x = new Add("hehe", "hoho", "haha", "hihi", "huhu"); var y = new Add("this", "is", "awesome"); var z = new Add("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"); x.print(); y.print(); z.print();

21st Feb 2021, 9:57 PM
Mark
Mark - avatar
2 odpowiedzi
+ 2
class Add { constructor(...words) { this.words = words; this.print = print; } } function print() { var a = this.words.map((word) => "
quot;+word); var b = a.join(""); console.log(b+"
quot;); } var x = new Add("hehe", "hoho", "haha", "hihi", "huhu"); var y = new Add("this", "is", "awesome"); var z = new Add("lorem", "ipsum", "dolor", "sit", "amet", ",", "consectetur", "adipiscing", "elit"); x.print(); y.print(); z.print();
22nd Feb 2021, 3:35 AM
SAN
SAN - avatar
+ 2
Mark you forgot to use curly brackets after for loop due to which only "blah=i-1" line is executed .
21st Feb 2021, 10:01 PM
Abhay
Abhay - avatar