# Help - Can't do this function exercise

Hi everyone,
I have tried to do this exercise but nothing works. Can anyone help me?

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'**
``````

Hi there, welcome to the forums.

Here are the steps you need to implement in your function:

1. Get the first item from the array.
2. Examine it to determine if it’s a character that you want to keep ("is it `a - z`, `A - Z`, or `0 - 9`?).
2.1 If YES, add the character to an output variable.
2.2 If NO, ignore the character.
3. Get the next item from the array, and repeat step 2. Do this for every item in the array.
4. When you’ve examined the entire array, return the results.

These steps are basically your algorithm, and you should be working this out - like I have done here - for any problem you attempt before you even think about writing code to do it.

There are a number of ways that you can convert this algorithm into a program; how is up to you.

Make an effort, and if you are still stuck post your attempt here and we’ll give you some pointers - but we won’t solve it for you.

I’ve also left a few hints in the algorithm, as suggestions of the type of thing I’d do if I were the one solving this.

Apart from what @thepitycoder says, you can try looking up the regular expressions guide of javacsript which can be used to match regular expressions. That’d solve your problem instantly.

Take a look at this link, it might help you solve the problem!
And if you’re still not able to solve it, you can always come back. We’re here to help!

1 Like

The fact that the example function calls

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

contain semi-colon statement terminators and comments with `//` suggests JavaScript to me, not Python.

Regex would be one way of solving this, though.

1 Like

Ah! I missed them. xD
I’ll make the changes right away!

I tried regular expressions and it worked.
But the output says > >>>>Code is incorrect
> RegEx’s are cool, but that is not the type of solution we are looking for

``````function extractPassword(array) {
return array.filter(e => e.match(/[a-z0-9]/i)).join('');
}

const result = extractPassword(['~', 'A', '7', '/', 'C']);
console.log(result);
``````

Ihave been learning javascript and I just learnt functions and loops. And i think this exercise is supposed to be done with a function and a loop.

1 Like

Oh, okay! If the solution shouldn’t consist of RegExs, you can try using the array itself to check if the character satisfies the condition or not. The implementation is correct using the .filter() method. But instead of checking using RegEx, try using the .includes() method of arrays in JavaScript. This is the link to the .includes() method’s documentation.
You can even try using the .find() method of arrays. Here’s the link to that documentation as well.

It was really nice to see you implement the solution in RegEx. Keep up the good work!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.