Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to convert an int into an array of digits in TypeScript

2 Answers

0 votes
let n: number = 840310;

let arr: string[] = n.toString().split('');
 
for (let i: number = 0; i < arr.length; i++) {
    console.log("arr[" + i + "] = " + arr[i]);
}
 
 
  
  
/*
run:
  
"arr[0] = 8" 
"arr[1] = 4" 
"arr[2] = 0" 
"arr[3] = 3" 
"arr[4] = 1" 
"arr[5] = 0" 
  
*/

 



answered Apr 4, 2024 by avibootz
0 votes
let n: number = 840310;

let arr: number[] = new Array(n.toString().length);
   
for (let i: number = arr.length - 1; i >= 0; i--) {
    arr[i] = Math.trunc(n % 10);
    n = n / 10;
}
 
for (let i: number = 0; i < arr.length; i++) {
    console.log("arr[" + i + "] = " + arr[i]);
}
 
 
  
  
/*
run:
  
"arr[0] = 8" 
"arr[1] = 4" 
"arr[2] = 0" 
"arr[3] = 3" 
"arr[4] = 1" 
"arr[5] = 0" 
  
*/

 



answered Apr 4, 2024 by avibootz
...