Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiskandar/8d172241a811850a9582dfc00b681ec6 to your computer and use it in GitHub Desktop.
Save andreiskandar/8d172241a811850a9582dfc00b681ec6 to your computer and use it in GitHub Desktop.
Repeating numbers
/*
Create a function named repeatNumbers that will return a string with each of the given
values repeated the appropriate number of times, if there are multiple sets of values
each set should be separated by a comma. If there is only one set of values then you
should omit the comma.
*/
const repeatNumbers = function(data) {
let str = '';
let tempStr = '';
if(data.length > 1) {
let len = 0;
while(len < data.length){
for(let i = 0; i < data[len][1]; i++){
tempStr += data[len][0];
}
str += `${tempStr}`;
tempStr = '';
if(len < data.length - 1) str += ', ';
len++;
}
return str;
} else {
for(let i = 0; i < data[0][1]; i++){
str += data[0][0];
}
return str;
}
};
console.log(repeatNumbers([[1, 10]]));
console.log(repeatNumbers([[1, 2], [2, 3]]));
console.log(repeatNumbers([[10, 4], [34, 6], [92, 2]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment