+ 4

[solved] JavaScript Code Project "Words"

Hi everyone, I have trouble solving the last Code Project in Javascript. I appreciate any hints or help. Thank you. The Problem: >>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

quot;. 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.<< My Code so far: class Add { constructor(...words) { this.words = words; } var wo = ""; for(var word : words){ wo += "
quot;+word; } wo += "
quot;; return wo; this.print = function(){ this.wo; } } 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")

19th Nov 2021, 9:28 AM
Lena Morawietz
Lena Morawietz - avatar
20 Réponses
+ 7
Lena Morawietz It's really nice that you are getting very complex answers without any explanation, but I wonder if you are learning anything from them. Might I suggest you revisit your understanding of the for loop, or use a while loop to iterate through the items presented to your function.
19th Nov 2021, 9:53 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
Lena Morawietz No need to use loop just use join function. console.log("
quot; + (this.words).join("
quot;) + "
quot;);
19th Nov 2021, 10:28 AM
A͢J
A͢J - avatar
+ 3
Your code showing error after closing class body check your loops Try this one class Add { constructor(...words) { this.words = words; } print(){ var y =""; for(x of this.words){ x=x.replace(/[^a-zA-Z ]/g, "") if(x!=""){ y += '
#x27;+x; } } console.log(y+'
#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(); y.print(); z.print();
19th Nov 2021, 9:36 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Rik Wittkopp I thought the same. The answer may help to solve the problem, but I want to find a solution I could learn from. Thanks for your suggestion, I will do that.
19th Nov 2021, 9:56 AM
Lena Morawietz
Lena Morawietz - avatar
+ 3
print() { //the function defination, wrap into this : var wo = ""; for(var word of this.words) // syntax : using of wo += "
quot;+word; wo += "
quot;; console.log(wo); //there is no catching in calling so print directly instead of returning.. } } edit: Lena Morawietz //' In ' predicate returns indexes .. //Hope it clears..
19th Nov 2021, 10:19 AM
Jayakrishna 🇮🇳
+ 3
Thank you so much. I didn't thought of the keyword "of". Now I really learnt something thanks to you :)
19th Nov 2021, 10:27 AM
Lena Morawietz
Lena Morawietz - avatar
+ 2
yes, i very much agree with Rik Wittkopp also you don't need to know everything in JavaScript to make a working program you just gotta have the basic understanding about js rest of the things are only meant to make your code easy and readable.
19th Nov 2021, 10:00 AM
zexu knub
zexu knub - avatar
+ 1
class Add { constructor(...words) { this.words = words; } print() { let word = this.words.map(i => i.concat('
#x27;)) return '
#x27; + word.join(''); } } 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();
19th Nov 2021, 9:45 AM
zexu knub
zexu knub - avatar
+ 1
I got a really weird output. Instead of the words it prints $0$1$2 and so on... What could be the reason? class Add { constructor(...words) { this.words = words; } print(){ var wo = ""; for(var word in this.words){ wo += "
quot;+word; } wo += "
quot;; console.log(wo); } }
19th Nov 2021, 10:17 AM
Lena Morawietz
Lena Morawietz - avatar
+ 1
Lena Morawietz It looks like you are iterating through the loop and attaching $ to each iteration. That's good, it's a step in the right direction. Now you need to review how to iterate through a list or string Keep going, you are close
19th Nov 2021, 10:22 AM
Rik Wittkopp
Rik Wittkopp - avatar
19th Nov 2021, 10:30 AM
ACE
ACE - avatar
+ 1
Hey gys, thank u so much for hint.
21st Nov 2021, 5:36 AM
Mohammed Rameez
Mohammed Rameez - avatar
0
Lena Morawietz Let us know how you go! 😁👍
19th Nov 2021, 9:57 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
We can solve by various way there is so many ways to do same like noob will get all word from words array and concate the $ sign and log it console but it will make our code ugly and more heavier so u should build algorithm then move with your algorithm.
20th Nov 2021, 7:02 PM
Nitin Singh
Nitin Singh - avatar
0
unique boss there is no way we can achieve it without iteration. even if we use buildin functions like join, concat, map or regex, it is internally getting iterated.
21st Nov 2021, 1:03 AM
ACE
ACE - avatar
0
【ACE】 The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator. You can use join also , map also just need to choose a way 😉. Join method it will return haha$haha$ if there is one item it will return haha only as per description so u should prefix with $ and you done like above given example which is upvoted for 7 times
21st Nov 2021, 3:04 AM
Nitin Singh
Nitin Singh - avatar
0
【ACE】 class Add { constructor(...words) { this.words = words } //print word print() { console.log("
quot;+this.words.join('
#x27;)+"
quot;); } } var a = new Add("hello", "world"); a.print(); var b = new Add("hello", "how", "are", "you"); b.print();
21st Nov 2021, 3:11 AM
Nitin Singh
Nitin Singh - avatar
0
unique boss i know how to use join, im just saying we can't concate array elements by seperator without iterating throught the elements. if we use join function then it is also iterating through the elements. Array.prototype.join implementation:- Array.prototype.myJoin = function(s = ""){ let temp = ""; for(let i = 0; i < this.length - 1; i++) { temp += this[i] + s; } return temp + this[this.length - 1]; } let arr = [1,2,3,4,5,6,7,8,9]; let str = arr.myJoin("->"); console.log(str); not only noob, even the computer itself would iterate through all the word of words. i just wanted to correct you there lol.
21st Nov 2021, 3:40 AM
ACE
ACE - avatar
0
【ACE】 hey man you can use length property to check array length and after that we can achieve the following by get array element with index but we have write long code for that without using iterator but we have to write more piece of line code , there is lots of way to solve one problem but we use efficient way to do so .
21st Nov 2021, 3:53 AM
Nitin Singh
Nitin Singh - avatar