Write a function called repeatWord that takes a
word and a number, and gives back that word glued together that many times.
repeatWordrepeatWord("ha", 3) gives back "hahaha"repeatWord("ha", 3) ➜ "hahaha"repeatWord("go", 5) ➜ "gogogogogo"repeatWord("meow", 1) ➜ "meow"
Try the finished version first so you know what you're building: open the Echo Machine demo ➜
A for loop repeats code a certain number of times:
for (let i = 0; i < times; i++) { ... }
Whatever is inside the { } happens times times!
Start with an empty text, and add the word to it each time around the loop:
let result = "";
result = result + word; (inside the loop)
Then don't forget to return result; at the end!
function repeatWord(word, times) {
let result = "";
for (let i = 0; i < times; i++) {
result = result + word;
}
return result;
}