Adding commas to string of numbers?

Hello,

I am writing a function to get the max and min numbers from the input (parameter).

The function works with no problem with the first code block below. The problem is it only works with a parameter that contains commas and now with a parameter such as ‘1 12 31’. The functions needs to be tested on parameters without commas. I have tried a number of things which haven’t worked, the latest of which is the tempComma var line included in the second text block below…

I know the tempComma var probably doesn’t make much sense, but not sure how to do this. Could someone give me a hint?

  let tempComma = numbers.replace(/\s/g, ", ");
  let tempArray = numbers.split(",").map(Number);

  console.log(tempArray);

  return `${Math.max(...tempArray)} ${Math.min(...tempArray)}`;
}
console.log(highAndLow("8 4 54"));
  let tempArray = numbers.split(",").map(Number);

  console.log(tempArray);

  return `${Math.max(...tempArray)} ${Math.min(...tempArray)}`;
}

instead of having these two lines, you can have your split with a regex as argument, and with that you can have a regular expression match all possible dividers between the numbers

thanks. I don’t know much (anything) about regex yet, but thanks for the tip, I will try to read up about that now…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.