I need to understand how with an array of numbers, divide it into blocks when the number zero appears, this marks the beginning and end of this and another block.
Which I must sort in ascending order and that when printing the sequences must be separated by a space where the zero was and if there is a sequence of zeros, X would be printed.
Example 1: [1,3,2,0,7,8,1,3,0,6,7,1]. The result: 123 1,378 167 Example 2: [2,1,0,0,3,4]. The result: 12X34.
var myArray = [1,3,2,0,7,8,1,3,0,6,7,1]
let salida ='';
let salida_temp = []
for(let i = 0; i < array.length; i++){
if(array[i] !== 0 && i+1 < array.length)
salida_temp.push(array[i])
else{
if(i+1 == myArray.length){
salida_temp.push(myArray[i])
}
Tank's
You have to go through series of consecutive steps to achieve the desired result.
function separateNumber(arr) {
return arr
.join("")
.replace(/(0)\1+/g, "0X0")
.split("0")
.map((s) => s.split("").sort().join(""))
.join(" ")
.replace(/ X /g, "X");
}
console.log(separateNumber([1, 3, 2, 0, 7, 8, 1, 3, 0, 6, 7, 1]));
console.log(separateNumber([2, 1, 0, 0, 3, 4]));
console.log(separateNumber([2, 0, 1, 0, 0, 3, 4]));
console.log(separateNumber([2, 1, 0, 0, 3, 4, 0, 0, 0]));