⬅ Back to homework list

📢 HW 3: Echo Machine

🎯 Your Mission

Write a function called repeatWord that takes a word and a number, and gives back that word glued together that many times.

Examples:
repeatWord("ha", 3)"hahaha"
repeatWord("go", 5)"gogogogogo"
repeatWord("meow", 1)"meow"

👀 See it working

Try the finished version first so you know what you're building: open the Echo Machine demo ➜

💻 Your Code

📊 Results

Press "Test my code!" to see your score.

🔍 Stuck? Open a hint

Hint 1

A for loop repeats code a certain number of times:
for (let i = 0; i < times; i++) { ... }
Whatever is inside the { } happens times times!

Hint 2

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!

🆘 Show me one answer

function repeatWord(word, times) {
  let result = "";
  for (let i = 0; i < times; i++) {
    result = result + word;
  }
  return result;
}