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);
11 Antworten
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();
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 $
0
AJ #Learn With Me 👨💻, would you mind showing me the syntax?
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);
0
AJ #Learn With Me 👨💻, code is still not working
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);
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.
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?
- 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 $.
- 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;;
- 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.