Can someone please help me solve this? The output needs to have a '</head>#x27; at the beginning and at the end of each word. i.e '$hehe</head>#x27; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone please help me solve this? The output needs to have a '
#x27; at the beginning and at the end of each word. i.e '$hehe
#x27;

class Add { constructor(...words) { this.words = words; } //your code goes here print(){ console.log(this.words); } } 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(this.words); y.print(this.words); z.print(this.words);

9th Dec 2020, 6:22 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
11 Answers
0
Update on the problem... I have managed to get the problem to work with some more help, here is the solution. class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var newWords = `${this.words.join('
#x27;)} ; console.log(newWords); } } 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();
11th Dec 2020, 11:58 AM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
Brandon Banks You need to add $ before and after the each word. Here three dot (...) represent spread operator so we do like ...words then words will be array. So you can use loop to append $
9th Dec 2020, 6:52 PM
A͢J
A͢J - avatar
0
AJ #Learn With Me 👨‍💻, would you mind showing me the syntax?
9th Dec 2020, 7:10 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
AJ #Learn With Me 👨‍💻, what am I doing wrong? class Add { constructor(...words) { this.words = words; } //your code goes here print(){ for(let i = 0; i < words.length; i++){ console.log("
quot; += words[i] += "
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(this.words); y.print(this.words); z.print(this.words);
9th Dec 2020, 7:35 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
AJ #Learn With Me 👨‍💻, code is still not working
9th Dec 2020, 7:58 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
AJ #Learn With Me 👨‍💻, this is my code with your assistance class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var newWords = ''; for(var i = 0; i < words.length; i++){ newWords = newWords + '
#x27; + words[i]; } newWords = newWords + '
#x27;; } 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(this.words); y.print(this.words); z.print(this.words);
9th Dec 2020, 8:11 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
0
Brandon Banks words is defined in constructor so you can access outside. To access you need to do this.words. Here this represents current object. learn about it.
9th Dec 2020, 8:14 PM
A͢J
A͢J - avatar
0
AJ #Learn With Me 👨‍💻, I am still a beginner... This challenge is difficult. Am I wrong for putting 'this.words' inside x.print(),y.print() and z.print()? I know that the "this" keyword refers to the current object?
9th Dec 2020, 8:47 PM
Brandon Lee Banks
Brandon Lee Banks - avatar
- 2
Brandon Banks There is not a special syntax. You just need to use loop and inside the loop use just need to concatenate each word with $ Here in the code words is an array. You can iterate and append $.
9th Dec 2020, 7:27 PM
A͢J
A͢J - avatar
- 2
Brandon Banks To concatenate string you just need + not += var newWords = ""; for (let I = 0; I < words.length; I++) { newWords = newWords + "
quot; + words[I]; } newWords = newWords + "
quot;;
9th Dec 2020, 7:46 PM
A͢J
A͢J - avatar
- 2
Brandon Banks Just write my code inside function and print output outside the loop. It will work. It is enough to tell. Give some effort.
9th Dec 2020, 8:04 PM
A͢J
A͢J - avatar