djtwdix
djtwdix

Reputation: 21

How to add commas to a string between characters that aren't the same?

The problem:

The input data for this exercise will be two dimensional array (an array of arrays), where each sub-array will have two numeric values.

The first will be the value to repeat, the second will be the amount of times to repeat that value.

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.

My code so far:

const repeatNumbers = function(arr) {
  let numbersRepeated = "";
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i][1]; j++) {
      numbersRepeated += arr[i][0];
    }
  }
  if (arr.length === 1) {
    return numbersRepeated;
  } else {
    return numbersRepeated;
  }
}

console.log(repeatNumbers([
  [1, 2],
  [2, 3]
]));

I am getting the correct output but obviously all in one string with no commas so far: running the above outputs:

11222

How can I make it so that a comma is inserted between the different numbers no matter what inputs are or how many sub arrays there are?

For the above example:

11, 222

Upvotes: 1

Views: 2175

Answers (4)

The fourth bird
The fourth bird

Reputation: 163352

Another option is to add the value to an array and the use join with a comma and space. That way you don't have to check for the length.

const repeatNumbers = function(arr) {
  let total = [];
  for (let i = 0; i < arr.length; i++) {
    let numbersRepeated = "";
    for (let j = 0; j < arr[i][1]; j++) {
      numbersRepeated += arr[i][0];
    }
    total.push(numbersRepeated);
  }
  return total.join(', ');
}

console.log(repeatNumbers([
  [1, 2],
  [2, 3]
]));

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

console.log(repeatNumbers([
  [99, 4]
]));

Upvotes: 1

trincot
trincot

Reputation: 350272

This seems a good case to use the native repeat method, and then map and join to concatenate those results into a comma separated string:

const repeatNumbers = (pairs) => 
    pairs.map(([what, count]) => String(what).repeat(count)).join();

// demo
console.log(repeatNumbers([ [123, 2], [87, 3] ]));

Upvotes: 1

charlietfl
charlietfl

Reputation: 171669

I would map() the array to new array of the repeating values and use join()

const repeatNumbers = function(arr) {
  return arr.map(e => e[0].toString().repeat(e[1])).join(', ')
}


console.log(repeatNumbers([
  [1, 2],
  [2, 3]
]));

Upvotes: 1

ADyson
ADyson

Reputation: 61904

Just insert the code to add the comma at the start of the outer looop (i.e. when it loops each set). But include a check for whether there is any previous text in the output, otherwise it'll add a comma before the first entry.

Like this:

const repeatNumbers = function(arr) {
  let numbersRepeated = "";
  for (let i = 0; i < arr.length; i++) {
    if (numbersRepeated != "") numbersRepeated += ",";
    
    for (let j = 0; j < arr[i][1]; j++) {
      numbersRepeated += arr[i][0];
    }
  }
  if (arr.length === 0) {
    return numbersRepeated;
  } else {
    return numbersRepeated;
  }
}

console.log(repeatNumbers([
  [1, 2],
  [2, 3]
]));

Upvotes: 2

Related Questions