Words | Sololearn: Learn to code for FREE!

0

Words

You are making a text encryptor. It should take multiple words and output a combined version, where each word is separated by a dollar sign $. For example, for the words "hello", "how", "are", "you", the output should be "$hello$how$are$you". The given code declares a class named Add, with a constructor that takes one rest parameter. Complete the code by adding a print() method to the class, which should generate the requested output. class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var y =""; for(x of this.words){ y += `$${x}`; } console.log(y); } } 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(); It's showing error why don't know, test is also locked

12/3/2020 2:43:18 PM

sm pravin

25 Answers

New Answer

+8

sm pravin Just do like for (x of this.words) { y += "$" + x; } y = y + "$";

+30

class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var y =""; for (x of this.words) { if(x == ","){ x = ""; } else{ y += "$" + x; } } y = y + "$"; console.log(y); } } 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();

+3

class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var i = 0; var text = ""; while(i <= this.words.length-1){ text += "$"+this.words[i]; ++i; } console.log(text+"$"); } } 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();

+1

i tried lots of ways but it still doesnt work and i dont know what is its test :((

+1

Zhenis Otarbay Try to understand the problem, you will get the solution.

+1

sm pravin it does not work for me

+1

There was a recent update to the supplied code, and it breaks older attempts. My old code once passed, but now fails. There is an easy fix. See my full explanation in this post. https://www.sololearn.com/Discuss/2630096/?ref=app

+1

https://code.sololearn.com/cYF3FiG25ocS/?ref=app it will work!

+1

print(){ this.words.push("$"); var newWords = this.words.map((word)=> "$"+word).join(''); var a = newWords.slice(0, -1); console.log(a); } }

0

I Am Groot ! How did it work?

0

I Am Groot ! What is the problem with this https://code.sololearn.com/cDB6vUmZdcZI/?ref=app

0

Zhenis Otarbay There is nothing wrong in your code even my code is also not working but I already solved. I think Sololearn added some new bug in this problem. Send mail.

0

I Am Groot ! How did you solved?

0

a simple one liner: console.log('$'+this.words.join('$')+'$');

0

class Add { constructor(...words) { this.words = words; } //your code goes here#print(){ print(){ var hey=""; for(var i=0; i<this.words.length; i++){ hey+="$"+this.words[i]; } console.log(hey+ "$"); } } 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

One line solution https://code.sololearn.com/cJigu352TpG5/?ref=app

0

class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var y =""; for (x of this.words) { if(x == ","){ x = ""; } else{ y += "$" + x; } } y = y + "$"; console.log(y); } } 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(); its working

0

class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var y =""; for (x of this.words) { if(x == ","){ x = ""; } else{ y += "$" + x; } } y = y + "$"; console.log(y); } } 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

This is what the print method should look like. This is what worked for me: print() { let y = ""; for (let x of this.words) { y+= `$${x}` } console.log(`${y}$`) }

-1

this worked for me class Add { constructor(...words) { this.words = words; } //your code goes here print(){ var y =""; for (x of this.words) { if(x == ","){ x = ""; } else{ y += "$" + x; } } y = y + "$"; console.log(y); } } 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();