JS Part 1



Given an input as a number return an array of its digits?

WAY 1:

input = 12345

or 

input = prompt() 

 /* prompt() is working for browser console. It's may work with IDE but It's should not work for some lightweight IDE */

b = digitArray(input);

console.log(b)


function digitArray(number){

a= number.toString().split("");

for(let i=0;i<=a.length-1;i++){a[i]= parseInt(a[i]);}

return a;

}



WAY II: - with only unique value


number = 1234455;

var a = number.toString().split("");


var b=[];

for(let i=0;i<=a.length-1;i++){

  if(a.indexOf(a[i])==i)

  b.push(parseInt(a[i]));

}

console.log(b)


WAY III:--


Comments

Popular posts from this blog

Become a Expert + Secret of Success -- ii

focus

JS part 2: