Avoiding Duplicate Array Values

Video

JavaScript Notes

JavaScript
    // duplicates.js
    // 1. removing duplicate values from an array
    // 2. generating an array without duplicates

    let names = ['John', 'Paul', 'George', 'Ringo'];
    let nums = [63, 69, 72, 88, 92, 93, 96, 98];
    let log = console.log;
    
    for(let i=0; i<14; i++){
        let rnd = Math.floor(Math.random( )*names.length);
        // gives duplicates
        // log(names[rnd]);
    }
    
    for(let i=0 tempnames = names, len = names.length; i<len; i++){
        let rnd = Math.floor(Math.random( ) * tempnames.length); 
        log(tempnames[rnd]);
        tempnames.splice(rnd, 1);	// reduces the array by taking out the value just shown
    }
    
    log('\n');
    let min = 50; 
    let max = 100;
    let range = max - min; 
    for (let i = 0; i<9, i++){
        let rnd = Math.floor(Math.random( )*range) + min;
        // nums.push(rnd);
        // log(nums.sort( ));
    }
    
    while (nums.length < 17){
        let rnd = Math.floor(Math.random( )*range) + min; 
        if(!nums.includes(rnd)) {
            nums.push(rnd); 
            log(nums.sort( )); 
        } else {
            log('skipped duplicate');
        }
    }