Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Put range of numbers in array as array of a single numbers

I want to put all numbers from min to max vars as a single number to array.
So in my example I want arr = [1,4,1,5,1,6] but i get [1,4, 1,5, ,1,6] as the result.
The problem is when i put separated values in array.
Would be very greatfull for any kind of help.

  let min = 14,
      max = 16,
      arr = [];

for (i=min; i<=max; i++) {
  arrPsh = i.toString().split("");
  arr.push(arrPsh);
}
for (i=0; i<arr.length; i++) {
  document.write(arr[i] + "<br>");
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can flatten the array:

let min = 14,
    max = 16,
    arr = [];

for (i=min; i<=max; i++) {
  arrPsh = i.toString().split("");
  arr.push(arrPsh);
}
arr = arr.flat();
for (i=0; i<arr.length; i++) {
  document.write(arr[i] + "<br>");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading