Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript [Help] Can't understand what I'm doing wrong...

infinito

New Coder
I'm doing a test for a Bootcamp. Stuck on this problem.

Here is the problem:

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:

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

MY CODE:

Code:
var extractPassword = function (array) {
    var validChar = [];
    for (var i = 0; i < array.length; i++) {
      if ('a' <= array[i] && array[i] <= 'z' || 'A' <= array[i] && array[i] <= 'Z' || '1' <= array[i] && array[i] <= '9') {
          validChar.push(array[i]);
      }
    }
    return validChar.join('');
};

console.log (extractPassword (['a', '~', '-', '1', 'a', '/'])); // console returns a1a correctly

ISSUE: The auto-correct of the test platform says "
>>>>Code is incorrect
Your function is not returning the correct value"


Can't use RegEx btw.

Can you see the issue? I really need to get into this Bootcamp. Thanks in advance.
 
I'm doing a test for a Bootcamp. Stuck on this problem.

Here is the problem:

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:

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

MY CODE:

Code:
var extractPassword = function (array) {
    var validChar = [];
    for (var i = 0; i < array.length; i++) {
      if ('a' <= array[i] && array[i] <= 'z' || 'A' <= array[i] && array[i] <= 'Z' || '1' <= array[i] && array[i] <= '9') {
          validChar.push(array[i]);
      }
    }
    return validChar.join('');
};

console.log (extractPassword (['a', '~', '-', '1', 'a', '/'])); // console returns a1a correctly

ISSUE: The auto-correct of the test platform says "
>>>>Code is incorrect
Your function is not returning the correct value"


Can't use RegEx btw.

Can you see the issue? I really need to get into this Bootcamp. Thanks in advance.
Hey there! Given the fact you are not allowed to use regex.. one way to go about it is check by character unicode value
//iterate through the array being passed in
//check to see if the character at the current index is within a range of unicode values for A-Z, a-z, 0-9.. and if so, add them to the results array

here is a great resource for ascii unicodes
 
I don't see the issue. First, that message from your autocorrect (?) does not seem to explain what exactly it think is wrong. Second, your console output seems to suggest the code is working as intended. Hmmm...
 
I think you meant
'0' <= array[index] && array[index] <= '9'
Well spotted ! I missed that.
Despite the fact JS executes && before ||, I keep thinking some parentheses would be good to have in this unwieldy if statement, if only for readability. Also, first doing var c = array[i]; and henceforth using c would make it much cleaner.
 
Well spotted ! I missed that.
Despite the fact JS executes && before ||, I keep thinking some parentheses would be good to have in this unwieldy if statement, if only for readability. Also, first doing var c = array[i]; and henceforth using c would make it much cleaner.
Yes, parenthesis are your best friends for life lol.
In your case,
without the parenthesis, your if statement is evaluating the && and || as part of one long chain
if( thisA AND thisB OR thisC AND thisD OR thisE AND thisF )
the logic: if thisA and thisB both have to be true, thisC can be either true or false, thisD has to be true, thisE can be either true or false, thisF has to be true...

with the parenthesis:
if ( ('a' <= array && array <= 'z') || ('A' <= array && array <= 'Z') || ('0' <= array && array <= '9') )

if( (thisA AND thisB) OR (thisC AND thisD) OR (thisE AND thisF) )
the logic: if thisA and thisB are both true, otherwise if thisC and thisD are both true, otherwise if thisE and thisF are both true



so, for readability, you actually want to be descriptive with your variable/function names. Makes it easier to follow along. Having names like var c = []... really doesn't help. We can obviously see that it is an array, but what is it for?
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom