0

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("");
}
Xjrlx00
  • 1
  • 1
  • 1
    You need _all_ of these conditions to be true to push the character to the array – Nick May 02 '23 at 01:18
  • If the str[i] is zero lets read it out loud. If zero does not equal zero OR zero does not equal one.... well zero does not equal one so it is true. You want AND not OR if you are going to do it this way. – epascarello May 02 '23 at 01:45

4 Answers4

5

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
AngYC
  • 3,051
  • 6
  • 20
  • Thanks so much for that explanation, that makes ALOT of sense. For some reason I thought that the OR (||) would still make it pass through the other statements. – Xjrlx00 May 02 '23 at 01:24
  • @Xjrlx00 Glad to hear that it helped you, all the best in your coding bootcamp and programming journey! – AngYC May 02 '23 at 01:28
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, '');
Ron
  • 5,900
  • 2
  • 20
  • 30
0

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?

Shaw
  • 148
  • 2
  • 6
  • I tried to do this originally, but it added a second space between 'Wnt' and 'Brak'.. unless I am doing something wrong there as well lol – Xjrlx00 May 02 '23 at 01:42
  • That's just the `2` being removed and the two spaces on either side remaining. You could either a) add another `replace()` to remove double white spaces: `string.replace(/[0-9]/g, '').replace(/\s+/g, ' ')` or b) trying altering the original regex, perhaps something like `string.replace(/[0-9]|\s[0-9]/g, '')` -- but I'm no regex expert! – Shaw May 02 '23 at 01:54
-1

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"
Tandera
  • 54
  • 3