-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplywordsstring.js
33 lines (23 loc) · 1.06 KB
/
multiplywordsstring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* You are to write an function that takes a string as it's first paramter. This string will be a string of words.
You are expected to then use the second parameter, which will be an integer, to find the corresponding word in the given string. The first word would be represented by 0.
Once you have the located string you are finally going to multiply by it the third provided paramater, which will also be an interger. You are additionally required to add a hyphen in between each word.
Example
modifyMultiply ("This is a string",3,5)
Should return
"string-string-string-string-string"
Since the 3rd word is 'string'(starting from 0 remember) and the third paramater indicates that it should be repeated 5 times.
Simple. Good luck. */
function modifyMultiply (str,loc,num) {
let arr = str.split(' ')
let res = []
let strres = ""
for (i =0; i<num; i++){
res.push(arr[loc]);
strres = res.join('-');
}
return strres
}
// additional way
function modifyMultiply(str,i,n) {
return Array(n).fill(str.split(' ')[i]).join('-');
}