-3
var a = 1;
var b = 05;
var c = 10;
var d = 14;
var e = 7;

I need to add zero to each variable if it is a single number.

Something like:

for each([a, b, c, d, e] as el){
if el.length == 1){el = '0' + el;}
}

Result should be 01 05 10 14 07

Any help?

qadenza
  • 9,025
  • 18
  • 73
  • 126

2 Answers2

5

You could convert the values to a string and pad with zero.

var a = 1,
    b = 05,
    c = 10,
    d = 14,
    e = 7;

[a, b, c, d, e] = [a, b, c, d, e].map(v => v.toString().padStart(2, 0));

console.log(a, b, c, d, e);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

This will solve your problem

var e=[1, 12, 3, 4, 23];
var o=[];
e.forEach((el)=>{

if (el/10<1)
{
var t = '0' + el.toString();
o.push(t);
}
else
o.push(el);
})
alert(o);
ellipsis
  • 12,049
  • 2
  • 17
  • 33