skip to Main Content

Given a string, return the string without numbers. For example: for the string, "I W4nt 2 Br3ak Fr33", it should return the string "I Wnt Brak Fr"

I am extremely new to this and have been in a coding boot camp with almost zero instruction to these more "complicated" functions.

I tried the following and in reverse I’ll get a string of just numbers but with non-equality I return the original string still..


function removeNumbers(str) {
  let arr = [];
  for (let i = 0; i < str.length; i++) {
    if (
      str[i] != '0' ||
      str[i] != '1' ||
      str[i] != '2' ||
      str[i] != '3' ||
      str[i] != '4' ||
      str[i] != '5' ||
      str[i] != '6' ||
      str[i] != '7' ||
      str[i] != '8' ||
      str[i] != '9'
    ) {
      arr.push(str[i]);
    }
  }
  return arr.join("");
}

4

Answers


  1. This is a logic error on your side, you would want to use AND operator (&&) instead of OR operator (||) in your case:

    function removeNumbers(str) {
      let arr = [];
      for (let i = 0; i < str.length; i++) {
        if (
          str[i] != '0' &&
          str[i] != '1' &&
          str[i] != '2' &&
          str[i] != '3' &&
          str[i] != '4' &&
          str[i] != '5' &&
          str[i] != '6' &&
          str[i] != '7' &&
          str[i] != '8' &&
          str[i] != '9'
        ) {
          arr.push(str[i]);
        }
      }
      return arr.join("");
    }
    

    Else, all characters will pass the validation.

    Let’s go through what happened during each loop for all the characters:

    1. str[i] is 4 (when you reach I W4 in I W4nt 2 Br3ak)
    2. This character 4 matches the first check str[i] != '0' and since this is using OR operator, it requires only 1 of the statements to be true to considered true, and as such it is pushed into the array.

    This is same for other numbers so all numbers are considered as valid, and is output in your final string.


    As a revision, a || b means that only either one of them needs to be true, in order to result in true.

    a && b means that both a and b has to be true, in order the result to be true.

    Truth table below for reference:

    a b a || b a && b
    Login or Signup to reply.
  2. Using regular expression:

    const regex = /[d]/g;
    
    // Alternative syntax using RegExp constructor
    // const regex = new RegExp('[\d]', 'gm')
    
    const str = `I W4nt 2 Br3ak Fr33`;
    const subst = ``;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);
    // result will be: 'I Wnt  Brak Fr'
    

    Playground: https://regex101.com/r/l2NJtN/1

    Or in short:

    const str = `I W4nt 2 Br3ak Fr33`; // or any other string
    const result = str.replace(/[d]/g, '');
    
    Login or Signup to reply.
  3. Just use the string replace() method, no need for arrays.

    const string = 'I W4nt 2 Br3ak Fr33';
    const newString = string.replace(/[0-9]/g, '');
    

    Everything between the slashes / is a regular expression. Here the pattern [0-9] matches all numbers, and the g makes it global so it matches all occurrences within the string. Then the empty string in the second argument '' replaces each number with nothing.

    Same as this question: How to remove numbers from a string?

    Login or Signup to reply.
  4. Most answers are too much complicated. Don’t worry, there’s other forms to solve this problem, without RegEx or long conditionals.

    First, create a list of numbers to be compared:

    const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    

    Then, create the removeNumbers function, iterating over all the characters of the string parameter.

    function removeNumbers(string) {
      var cleaned_string = "";  // Here the String without numbers will be stored //
    
      for (var c = 0; c < string.length; c++) {
        // for each character of the String //
      }
    
    }
    

    Inside this for loop, let’s check if the character of the iteration is not a number with the function includes:

    if (!numbers.includes(string[c])) { // This will only be executed if the character is not a number // }
    

    Then, inside in this if, concatenate the current letter on the cleaned_string variable.

    if (!numbers.includes(string[c])) {cleaned_string = cleaned_string + string[c];}
    

    Finally, return the cleaned_string value:

    return cleaned_string;
    

    Complete code:

    const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    
    function removeNumbers(string) {
      var cleaned_string = "";  // Here the String without numbers will be stored //
    
      for (var c = 0; c < string.length; c++) {
        // for each character of the String, do:
        if (!numbers.includes(string[c])) {  // If the 'string[c]' (character) IS NOT ("!") in 'numbers';
          cleaned_string = cleaned_string + string[c];  // add to the cleaned version.
        }
      }
    
      return cleaned_string;
    }
    
    const test_string = "I W4nt 2 Br3ak Fr33";
    console.log(removeNumbers(test_string));  // "I Wnt  Brak Fr"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search