I am having some issues solving this problem, and would appreciate if someone could provide guidance on the best way to go about solving it. Thank You.
Write a function onlyLetters that takes a string and returns a new string with the numbers filtered out.
Examples:
onlyLetters('12ab') // => 'ab'
onlyLetters('1xz015')// => 'xz'
onlyLetters('1aasf123ql') // => 'aasfql'
Here is what I wrote:
function onlyLetters(str) {
if(str === isNaN === true) {
return str;
}
} onlyLetters('12ab');
function onlyLetters(input) {
/*
*Check for characters 0->9 globally in a regex check
*and replace them to '' or nothing.
*/
return input.replace(/[0-9]/g, '');
}
/*
* Single line solution (if necessary)
* let onlyLetters = (input) => input.replace(/[0-9]/g, '');
*/
console.log(onlyLetters('12ab')); // => 'ab'
console.log(onlyLetters('1xz015')); // => 'xz'
console.log(onlyLetters('1aasf123ql')); // => 'aasfql'
How about something as simple as replace? It may be too simple and for that maybe ask another question?