Javas

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:

var password = ['a','º','~','z','A','&','Z','0','*','9'];
var newPass = [];
var arrayLength = password.length;

function extractPassword(password) {
   
    for (var i = 0; i < arrayLength; i++) {
        var j = password[i];
        
       
        if (('a' <= j && j <= 'z')|| ('A' <= j && j <= 'Z')|| ('0' <= j && j <= '9')) { 
            newPass.push(j);         
        }
      
    }
    return newPass.join(''); 
 }
extractPassword(password);
console.log(newPass);```

// output:

[
"a",
"z",
"A",
"Z",
"0",
"9"
]



I´m stuck on this for 2 days, I´m getting blind ,I rewrite my code a lot of times ,but I still can't see what am I doing wrong....

Hi:
What’s wrong with your code? That output looks like what you wanted.

What is the error message you’re getting?

Hi,

this is the error message:

Code is incorrect
Your function is not returning the correct value

Ok! Let’s try to figure this out.

Have you studied regex yet? Do you know if they want you to use that instead? Can you link to the lesson so people can try to replicate this issue?

Are you printing the output as well (sometimes the autograder can get conflicted with this?)?

nop they didn’t talk about regex yet…

If you have a moment, please link to the exercise :slight_smile:

// the exercise:

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’

No I mean actual url link, so we can try to replicate the issue and find if there’s a potential bug.

1 Like

Ah this is non-cc.

I can’t report bugs for that :slight_smile:

But what I would say then is that you basically have the code correct (I would do a bit more testing with dummy passwords just in case, looking for edge-cases).

Don’t get stuck on this just because the grader is being picky. If it’s something you can report or inquire with that website’s team, I would do that.

There’s also tons of places where solutions to this topic are discussed in depth. Just google javascript alphanumeric filter and you’ll have plenty of discussions to look at.

1 Like

Tks for the hint!!! I ´will keep on trying!

Your function pushes the valid characters to the array, newPass. It then returns the array elements joined together to form a string, but the return value is not stored anywhere. My best guess is that the value logged to the console at the end is supposed to be a string rather than an array. It is also probably supposed to be the value returned by the function. Whatever code is used to grade your code is almost certainly calling your function with a password argument, and expecting the corrected password to be returned (seemingly as a string).

Edit: Guess I need more coffee. Your code does return the corrected password as a string. Assuming that my assumption regarding the grading software is correct, your code would pass if it’s only run once, but what if it is run twice or more? Try it, and see what you get returned:

var password = ['a','º','~','z','A','&','Z','0','*','9'];
var newPass = [];
var arrayLength = password.length;

function extractPassword(password) {
   
    for (var i = 0; i < arrayLength; i++) {
        var j = password[i];
        
       
        if (('a' <= j && j <= 'z')|| ('A' <= j && j <= 'Z')|| ('0' <= j && j <= '9')) { 
            newPass.push(j);         
        }
      
    }
    return newPass.join(''); 
 }
extractPassword(password);
console.log(newPass); //prints the array not the return value
//add these lines, and run your code:
console.log(extractPassword(['a', '-', '~', '1', 'a', '/'])) //prints the return value, but probably not what you are expecting
console.log(extractPassword(['~', 'A', '7', '/', 'C'])) //same here
1 Like

I think I´m not getting it…

Here’s the output when we add 2 additional calls to your function:
Output:

[ ‘a’, ‘z’, ‘A’, ‘Z’, ‘0’, ‘9’ ]
azAZ09a1a
azAZ09a1aA7C

The instructions state that the second and third calls should have returned:

Your code returns 'azAZ09a1a' when 'a1a' is expected.
It then returns 'azAZ09a1aA7C' when 'A7C' is expected

As I understood what is asked is:
‘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).’

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

works as an example…I´m gonna study more about getting strings from return, to try to get over this :sob:

tks a lot for the support!

Your function does what it’s supposed to. The issue is that calling it repeatedly is adding to the array you are pushing valid characters to. Instead of the desired output, your function is returning the value from the first time it is called with the value from the next time appended to the end. This process continues with each subsequent function call. Notice in my last post that the output gets longer with the expected value at the end. You probably want to start with an empty array each time the function is called. An empty string would work just as well, and eliminate having to join the array elements.