The input data is a two-dimensional array, in which each nested array will have two numeric values. The first will be the value to repeat, the second will be the number of repetitions of this value.

let repeatNumbers = function(data) { // Put your solution here }; console.log(repeatNumbers([[1, 10]])); console.log(repeatNumbers([[1, 2], [2, 3]])); console.log(repeatNumbers([[10, 4], [34, 6], [92, 2]])); 

Expected Result:

 1111111111 11, 222 10101010, 343434343434, 9292 

The function must return a string in which each of the specified values ​​is repeated a corresponding number of times, each set of values ​​is separated by a comma. If there is only one set of values, skip the comma.

I only thought of a solution for the first subarray, how to decide when there are a lot of subarrays until I apply my mind (

 let repeatNumbers = function(data) { let firstEl = data[0][0].toString(); return firstEl.repeat(data[0][1]); } console.log(repeatNumbers([[1, 10]])); console.log(repeatNumbers([[1, 2], [2, 3]])); console.log(repeatNumbers([[10, 4], [34, 6], [92, 2]])); 

    1 answer 1

    for traversing the external array, use map() , for repetitions - repeat() , for commas - join() . that's all.

     var data = [[10, 4], [34, 6], [92, 2]]; var result = data.map(v => v[0].toString().repeat(v[1])).join(','); console.log(result); 

    • Here I am a village - I didn’t think of it (everything turned out to be so simple. It’s hard to be a novice. Thank you very much @teran. - Anna