0

Hi i recently did a number generator and i have an array filled with 0, 0, 0, 0, 0, because it generates numbers from 0 - 99999 and i want to make it replace only a few elements, for example if it generates 123 then i want an array to be [0, 0, 1, 2, 3], if 4467 then [0, 4, 4, 6, 7] etc any suggestions? thats the code i have for array:

let randInt = Math.floor(Math.random() * 100000);
let separated = [0, 0, 0, 0, 0];

separated = Array.from(String(randInt), Number);
2
  • 1
    For the mathematical way division / and modulo % will help. The other possibility is to use randInt.toString().split("") and then add the missing zeros at the beginning of the array Commented Oct 4, 2022 at 13:52
  • 5
    or radInt.toString().padStart(5, 0).split('').map(Number) Commented Oct 4, 2022 at 13:55

4 Answers 4

3

Here's how you do it

  • convert the number to a string
  • pad the string with leading "0" so it is 5 characters long
  • split
  • map to Number
  • profit

OK, the last step is optional :D

// set to 123 to demonstrate
const randInt = 123;// Math.floor(Math.random() * 100000);
const separated = randInt.toString().padStart(5, 0).split('').map(Number);
console.log('Output:', separated);

Another alternative

  • add 100000 to the random number, therefore you get numbers 100000-199999
  • toString
  • split('')
  • slice(1) (to remove the leading 1)
  • map to Number

note: the split('').slice(1) can also be slice(1).split('')

So really, that's two more solutions :p

const randInt = Math.floor(Math.random() * 100000 + 100000);
const separated = randInt.toString().split('').slice(1).map(Number);
console.log(randInt, separated);

Sign up to request clarification or add additional context in comments.

Comments

2

If you want to use a full string, you can use padStart to add in leading zeros. You can split it and map it to the new array.

const separated = Math.floor(Math.random() * 100000).toString().padStart(5, '0').split('').map(Number);

console.log(separated);

You can also just do it from Array.from

const nums = Array.from( {length: 5}, () => Math.floor(Math.random()*10));

console.log(nums);

Comments

1

Hope it'll be helpful.

 const randomNum = Math.floor(Math.random() * 100000); 
 console.log('randomNum is ' + randomNum);
 let finalResult = [0,0,0,0,0];
    let result = randomNum.toString().split('');
    result = result.map(num=>parseInt(num));
    const gap = finalResult.length - result.length; 
    if(gap>0){
      for(let i = 0; i< finalResult.length - gap; i++){
        finalResult[i+gap] = result[i];
      }
    }
    else
      finalResult = result;
    console.log(finalResult);

4 Comments

so, 4 digit numbers up to 9998, instead of 5 up to 99999 ... and still no leading zeros
this produces ['9'] instead of ['0', '0', '0', '0', '9'] if randomNum === 9
@derpirscher - it only ever produces 4 digits anyway
Math.random is inclusive, exclusive, so it should be Math.random() * 100000. Some of your lets should be consts (randomNum; gap).
0

Leveraging lodash utility padStart and includes logic for variable size max:

import { padStart } from 'lodash';

const MAX = 1000;

let randInt = Math.floor(Math.random() * MAX);
const padTo = MAX.toString().length - 1

const paddedNumArr = (padStart(randInt.toString(), padTo, '0'))
      .split('')
      .map(i => parseInt(i));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.