1

I've been trying to figure this problem out for a while but I'm at a blank. Here's what I have so far:

var repeatNumbers = function(data) {
   var repeated = [];

   for ( var x = 0; x < data.length; x++){
     var unit = data[x][0]
     var quant = data[x][1]

     for(var i = quant; i > 0; i--){
       repeated.push(unit);
       repeated.join(',');
     }

     return repeated;
  }
};

console.log(repeatNumbers([1, 10]));

Basically I'm trying to repeat the first number of the array based off of the second value. Any insight would be greatly appreciated thank you! :)

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
Cristof.M
  • 25
  • 7
  • You might consider using consistent indentation when writing code - it'll make read and debugging it much easier, not only for potential answerers, but for you as well, when we can all see the `{` `}` blocks and their nesting level at a glance. Can you give a concrete example of an input and the expected output? – CertainPerformance Dec 31 '18 at 02:32
  • Do you mean to say that you want the 0th item to be the same as the 1st item in an array? This sounds trivial? Can you explain your situation a bit better? – seebiscuit Dec 31 '18 at 02:37
  • Is the expected output an array of 1’s repeated 10 times? – Jack Moody Dec 31 '18 at 02:38
  • Sorry for the lack of professionalism with my post it's my first time using a forum site. My expected output would be 1 repeated 10 times in an array in this case. – Cristof.M Dec 31 '18 at 02:39
  • Can you post a concrete example (code output, not words)? eg `[1,1,1,1,1,1,1,1,1,1]` or what? – CertainPerformance Dec 31 '18 at 02:41
  • Yes exactly that [1,1,1,1,1,1,1,1,1,1] – Cristof.M Dec 31 '18 at 02:41
  • `([ length, item ]) => new Array(length).fill(item)` – CertainPerformance Dec 31 '18 at 02:44
  • `Array(10).fill(1);` or `Array.from({length:10}, i => 1);` or `Array.from({ length: 10 }).fill(1);` – m fauzan abdi Dec 31 '18 at 02:49

2 Answers2

0

You don't need to loop over your array if you only have two numbers, where the first number (at index 0) is the number you want to repeat, and the second number is the number of times you want to repeat that number (index 1).

Once you have the number of times you wish to repeat the number, you can simply use a for loop to enter the number into your repeated array that number of times.

See working example below (read code comments for further explanation):

var repeatNumbers = function(data) {
  var repeated = []
  
  var toRepeat = data[0]; // get the first number in the array
  var times = data[1]; // get the second number in the array
  
  // loop the number of times you want to repeat the number:
  for(var i = 0; i < times; i++) {
    repeated.push(toRepeat); // push the number you wish to repeat into the repeated array
  }
  
  return repeated.join(); // return the joined array (as a string - separated by commas)
}
console.log(repeatNumbers([1, 10]));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

If I understand your question correctly, you want the function repeatNumbers() to return an array with the first element in the passed array replicated by the second element in the passed array.

To achieve that, you could do the following:

var repeatNumbers = function(data) {

  // Extract the "value" to be repeated, and the "repeated" value
  // that will control the number of "value" items in result array
  var value = data[0];
  var repeated = data[1];

  var result = []

  // Loop over repeated range, and push value into the result array
  for (var i = 0; i < repeated; i++) {
    result.push(value);
  }

  // Result result array
  return result;
};

console.log(repeatNumbers([1, 10]));

Or, if you don't need to support IE, a more consise approach would be:

var repeatNumbers = function(data) {

  var value = data[0];
  var repeated = data[1];
  
  return (new Array(repeated).fill(value));
};

console.log(repeatNumbers([1, 10]));
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65