0

Hello there StackOverflow people,

What I expected:
Removing the numbers of the string "23Ka5X". The loop counts the length and the if statement extracts the letters into an array letterMemory. When no letters are in the string, the message '"oh no numbers!" should be the output.
What I ran into:
I have been working on this for some time now but I can't find my mistake. I don't know if I missed a simple detail or made a big mess. My feeling and console output:

var letterMemory = [];
    function orderMsg(mixedMsg) {
        for (var loopString = 0; loopString < mixedMsg.length; loopString++); {
          if (isNaN(parseInt(mixedMsg.charAt[loopString]))); {
           letterMemory.push(mixedMsg.charAt[loopString]);
           return letterMemory;  
        } if (!isNaN(parseInt(mixedMsg.charAt[loopString]))) {
           return "oh no numbers!";
        }
      }
    }
    console.log(orderMsg("23Ka5X"));

I feel like the issue is trying to push any letter into the array letterMemory via letterMemory.push(mixedMsg.charAt[loopString]) does not work how I imagine it.

I would be really grateful for your help!

I found a simple solution via .replace() but I really want to make it work with a loop and if statements since loops combined with if statements were my latest freecodecamp lessons and I want to get better at it.

2
  • 2
    Use the return statement outside for loop. Commented Sep 8, 2018 at 20:27
  • .charAt is a function so needs regular parentheses, not square brackets. Also isNaN(parseInt(...)), the parseInt isn't necessary as '3' for example is a number, even if it is in a string Commented Sep 8, 2018 at 20:36

5 Answers 5

1

The fixed code

function orderMsg(mixedMsg){
    var letterMemory = []
    for (var loopString = 0; loopString < mixedMsg.length; loopString++){
        if (isNaN(mixedMsg[loopString])){
            letterMemory.push(mixedMsg[loopString])
        }
    }

    if (letterMemory.length){
        return letterMemory
    } else {
        return 'oh no numbers!'
    }
}

The issue was

  1. The for loop was not executing since you terminated it with ; at the end.
  2. charAt is a function, so you either do string.charAt(index), or you can also simply say string[index].
  3. You are using the return statement within the for loop, so what will happen is even if the for loop ran (without the semi-colon at the end), it would run just once.
  4. One other issue is that the variable letterMemory is declared outside the function so that means if you were to call this function twice, it would use the same letterMemory array.

-end of answer-

Additional read: you can use split, filter and ternary operator to condense the function as follows ..

function orderMsg(mixedMsg){
    const letterMemory = mixedMsg.split('').filter(isNaN)
    return letterMemory.length ? letterMemory : 'oh no numbers!'
}
Sign up to request clarification or add additional context in comments.

Comments

0

This could be helpful,

function orderMsg(mixedMsg) {
    for (var loopString = 0; loopString < mixedMsg.length; loopString++) {
        if (isNaN(parseInt(mixedMsg.charAt(loopString)))) {
            letterMemory.push(mixedMsg.charAt(loopString));
        }
    }
    return letterMemory;
}

var arr = orderMsg("23s5");
if (arr.length == 0) {
     console.log("oh no numbers!")
} else {
     console.log(arr);
}

Comments

0

Use replace with regex globally, replacing all digits by an empty string:

string.replace(/[0-9]/g, "")

1 Comment

Sorry, I've just read your last comment, but I'll leave this answer if anyone needs this solution.
0
  1. You have terminated for loop in the same line with ;.
  2. charAt() is a method.
  3. Return value after for loop ends.

    var letterMemory = [];
    function orderMsg(mixedMsg) {
        for (var loopString = 0; loopString < mixedMsg.length; loopString++) {
            var letter=parseInt(mixedMsg.charAt(loopString));
            if(isNaN(letter)){
                letterMemory.push(mixedMsg.charAt(loopString));
            }
        }
        if(letterMemory.length>0){
            return letterMemory;
        }
        else{
            return "Oh no numbers!";
        }
    }
    console.log(orderMsg("23Ka5X"));
    

Comments

0

Maybe try using .test to match the letters.

function orderMsg(str){
  var result = [];
  for(var letter of str){
    if(/[a-zA-Z]+/g.test(letter)){
      result.push(letter)
    }
  }
  if(result.length === 0){
    return 'Oh no numbers'
  }
  return result
}

For a more thorough explanation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.