Padding Methods

Video

JavaScript Notes

JavaScript
    /***
     * Padding Strings
     * 
     * padStart(len[, filler])
     * padEnd(len[, filler])
     * 
     * Create an id that needs to be a specific length
     * Tab delimited values
     * Hex value random colours # F10F0F
     */

    let log = console.log;
    
    // 1. id numbers that have to be 9 digits
    let id = 432177;
    //log(id.toString().padStart(9, '0'))
    //increase length to 9 characters. Use '0' to pad
    
    // fixed length file format
    let userid = 123456; //first 10 chars
    let user = 'Bob'; //next 10 chars
    let email = 'bob@work.org'; //final 30 chars
    let fullLine = userid.toString().padStart(10, '0') + user.padStart(10) + email.padStart(30);
    
    //log(fullLine);
    
    //random hex colours
    for (let i = 0; i < 25; i++) {
        let red = Math.floor(Math.random() * 255).toString(16);
        let green = Math.floor(Math.random() * 255).toString(16);
        let blue = Math.floor(Math.random() * 255).toString(16);
        let colour = '#' + red.padStart(2, '0') + green.padStart(2, '0') + blue.padStart(2, '0');
        log(red, green, blue, colour);
    }