4

I was given this string:

var myMessage = "Learning is fun!"

This is how I attempted to create an array listing only the letters (without the spaces and "!").

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
    let arr2 = []
    for(let i = 0; i < array.length; i++){
        if(array[i] === "a" || "b" || "c" || "d" || "e" 
        || "f" || "g" || "h" || "i" || "j" || "k" || "l" 
        || "m" || "n" || "o" || "p" || "q" || "r" || "s" 
        || "t" || "u" || "v" || "w" || "x" || "y" || "z"){
          arr2.push(array[i])
        }
    }
    return arr2
}

console.log(onlyLetters(myMessage))

What am I doing wrong? Also, is there a shorthand for listing letters "a" through "z"?

Yumi Park
  • 41
  • 1
  • 1
  • 4
  • You can't do `array[i] === "a" || "b" || .... ` because the right side of the `===` is evaluated as `"a" || "b" || .... `. What you mean to do is `array[i] === "a" || array[i] === "b" || ...` – ninesalt Oct 06 '19 at 17:23

4 Answers4

7

A simple way may be to use Regex like so

let message = "Learning is fun!";
let onlyLettersArray = message.split('').filter(char => /[a-zA-Z]/.test(char));
console.log(onlyLettersArray)

.filter takes an array and runs a function on the elements, which returns true or false. The item is removed if it returns false. The regex checks if the character is within the range a-z or A-Z

Another way is to filter the char and then split it like so

let message = "Learning is fun!";
let onlyLettersArray = message.replace(/[^a-z]+/gi, '').split('');
console.log(onlyLettersArray)

Edit:

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
    let arr2 = []
    for(let i = 0; i < array.length; i++){
        if(/[a-z]/.test(array[i])){ // you can use regex instead of all characters
          arr2.push(array[i])
        }
    }
    return arr2
}

console.log(onlyLetters(myMessage))

Update: If instead of an array of characters, you have to replace special chars in a string, you can write

let message = "Learning is fun!";
let letterMessage = message.replace(/[^a-zA-Z]/gm,"")
console.log(letterMessage)


Dhananjai Pai
  • 5,914
  • 1
  • 10
  • 25
2

You simply can use String.prototype.match to get the array of letters only.

let arr = "Learning is fun!  1233  ashdgahsgdh".match(/[A-Za-z]/g);
console.log(arr)
AZ_
  • 3,094
  • 1
  • 9
  • 19
0

You could take a lower case string, split it and check each character if the value is greater or equal to 'a' and smaller or equal to 'z'.

Ath the end, after filtering, you could get a string back by joining the array with an empty string as glue.

function onlyLetters(string) {
    return string
        .toLowerCase()
        .split("")
        .filter(c => c >= 'a' && c <= 'z')
        .join('');
}

console.log(onlyLetters("Learning is fun!"));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Since OP is a beginner, I would propose another solution that doesn't use regex

var myMessage = "Learning is fun!";
var arr1 = myMessage.split("");

function onlyLetters(array){
let arr2 = []
for(let i = 0; i < array.length; i++){
    if(isLetter(array[i])){
      arr2.push(array[i])
    }
}
   return arr2
}

function isLetter(c) {
   return c.toLowerCase() != c.toUpperCase();
}
packability
  • 364
  • 1
  • 2
  • 10