add zero to each variable having a single number

62
January 11, 2019, at 12:50 PM
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?

Answer 1

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);

Answer 2

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);
READ ALSO
Combining two arrays side by side in Angular 7

Combining two arrays side by side in Angular 7

To combine two arrays side by side, I am following the below procedure but I get

73
Mongoose: Remove Document and all references completely

Mongoose: Remove Document and all references completely

I was wonderingI have a lot of coding to do with a function to remove a document from the database

57
hyperledger fabric calling enrollment endpoint failed with error

hyperledger fabric calling enrollment endpoint failed with error

I was trying to deploy hyperledger fabric on multiple hosts using this code and used the balance transfer chaincode using node-SDKWhile I tried to enroll the user, I got the following error:

84
HTML Table to Excel Issue

HTML Table to Excel Issue

I'm using the TableExportjs API and PNP

22