dimanche 1 mars 2015

Create a string that repeats itself x number of times

I want to create a function that returns a string a specific number of times. I have tried with this.



function stringRepeat(word, number) {
var string = ””
for(i= 0; i < number; i++) {
string += word;
}
}
console.log(stringRepeat("grey", 12));


However, it doesn´t do the job. What should I change?


I have tried to change ”string += word;” to ”console.log(word)” and then it executes (but i get the ”word” written on 12 different lines- I want to have it in one long string.)


When I execute the existing code I get ”undefined” as the result. Why is this?


Thanks in advance!


Aucun commentaire:

Enregistrer un commentaire