Home > Software design >  Why does one function work, but the second with other variables doesn't?
Why does one function work, but the second with other variables doesn't?

Time:12-03

Question:
I have two functions in my code below. They are supposed to pick a random out of an array for var amount times. Then delete that random out of the array.

The first function for 2 random numbers works, but the second one, for lowercase letters, doesn't.

I tried:
I tried looking at both functions but they look the same to me, only different variables...

(This is a little part of a code that creates an random password.)

// Needed vars
var numbersN = [
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9'
];
var lowercaseN = [
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z'
];
var allN = numbersN.concat(lowercaseN);
var password;
var extra_safe = true;

// function randomNumbers (Works!)
var amountNumbers = 2;
function randomNumbers (){
    for (var a = 0; a < amountNumbers; a = a   1){
        var random = pickRandom(numbersN);
        password = password   random;
        if (extra_safe === true){
            console.log(numbersN);
            delete numbersN[random];
            console.log(numbersN);
            delete allN[random];
        }
    }
}



// function randomLowercase (Doens't work..)
var amountLowercase = 2;
function randomLowercase (){
    for (var b = 0; b < amountLowercase; b = b   1){
        var random = pickRandom(lowercaseN);
        password = password   random;
        if (extra_safe === true){
            console.log(lowercaseN);
            delete lowercaseN[random];
            console.log(lowercaseN);
            delete allN[random];
        }
    }
}

// Runs function   TEST: prints numbersN before and after the working delete
randomNumbers();

// Runs function   TEST: prints lowercaseN before and after the failed delete
randomLowercase();

// TEST: prints allN, this should show all numbers and lowercase letters except the deleted randoms (It only works for the numbers.)
console.log(allN);

// prints the random numbers   lowercase letters
console.log(password.replace(/['undefined']/g, ''));

Thanks!

Adriaan V

Please note, I'm a beginner and English is not my native language. I'm sorry for the spelling mistakes and my simple code. Please leave suggestions for better tags etc.

CodePudding user response:

The issue with the randomLowercase function is the use of the delete keyword. The delete keyword is used to delete properties from objects. Since the lowercaseN and allN variables are array of strings, not objects, the delete keyword does not work as intended.

You could then determine the index of the selected letter, and use the splice() method to remove the element at that index in the array.

MDN page on the delete operator here.

  • Related