Gerdo
Gerdo

Reputation: 3

Javascript - extract letters from an alphanumerical string via loop

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.

Upvotes: 0

Views: 203

Answers (5)

rmn
rmn

Reputation: 1169

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!'
}

Upvotes: 1

I0_ol
I0_ol

Reputation: 1109

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

Upvotes: 0

Atal Prateek
Atal Prateek

Reputation: 541

  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"));
    

Upvotes: 0

Felipe Micali
Felipe Micali

Reputation: 847

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

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

Upvotes: 0

benjamin c
benjamin c

Reputation: 2338

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);
}

Upvotes: 0

Related Questions