Extract characters from arrays

Hi, i have this challenge to solve, and I´m stuck for 2 days now…I´m reading a lot of stuff, and I see I can use filter method or match, but we didn’t talk about them in this platform yet, so I try different approaches, but all wrong, can anyone help me?

// the challenge:

Create a function named extractPassword which takes an array of characters (which includes some trash characters) and returns a string with only valid characters (a - z, A - Z, 0 - 9).

Here’s an example:

extractPassword([‘a’, ‘-’, ‘~’, ‘1’, ‘a’, ‘/’]); // should return the string ‘a1a’
extractPassword([’~’, ‘A’, ‘7’, ‘/’, ‘C’]); // should return the string ‘A7C’

// my code:

extractPassword(['a','º','~','z','A','&','Z','0','*','9']); {
    var newPass = []; 
    for (var i = 0; i < extractPassword.length; i++) {
        var j = extractPassword[i];
        if (('a' <= j && j <= 'z')|| ('A' <= j && j <= 'Z')|| ('0' <= j && j <= '9')) { 
            newPass.push(j);
        }
    }
    return newPass.join();
}

// output

Code is incorrect
syntax error
Illegal return statement

the illegal return statement is illegal because it is not inside a function

you are not creating a function there

I’ve edited your post to show the first line

the code starts on the next line, not on the same line than backticks

1 Like

the function calls used to test your function have an argument, meaning that they should use the argument and give different output for different input

your function gives always the same output and is not reusable

do not use global variables, those contribute to make your function mot reusable

your arrayLength is always the same, it should change depending on what is passed to the function