Home > other >  How to remove a character from a string without using native methods nor loops in Javascript?
How to remove a character from a string without using native methods nor loops in Javascript?

Time:02-22

I need to remove a character at a time from a string, without using native methods or loops. I've found countless ways to do this without native methods OR without loops, but can't seem to find a way to do without either. I was able to come up with a way to use recursion to avoid loops, but not sure how to not use the string.slice() method. I must use recursion. No this is not a homework problem, I'm just practicing for an interview.

The function is supposed to count how many times the target character is repeated (if at all) in an input string.

const countChar = (input, target) => {
    if (input === '') return 0;
    if (input[0] === target) return 1   countChar(input.slice(1), target);
    return countChar(input.slice(1), target);
}

console.log(countChar('hello world', 'o')); // 2
console.log(countChar('javascript', 'j')); // 1

CodePudding user response:

The main thing that your current code is using .slice() for is to "trim" your string down so that on the next recursive call the first character of your input is the next character that you need to check. Instead, you can pass the index of the current character that you want to check, allowing you to avoid the slice call:

const countChar = (input, target, i = 0) => {
    if (i >= input.length) return 0;
    if (input[i] === target) return 1   countChar(input, target, i 1);
    return countChar(input, target, i 1);
}

console.log(countChar('hello world', 'o')); // 2
console.log(countChar('javascript', 'j')); // 1

There are of course methods other than .slice() that you could use to break up your string, but these still involve "looping"/iterating your string under the hood. One example is to destructure:

const countChar = ([c, ...rest], target) => 
  c ? (c === target)   countChar(rest, target) : 0;

console.log(countChar('hello world', 'o')); // 2
console.log(countChar('javascript', 'j')); // 1
console.log(countChar('', 'j')); // 0

The above snippet uses destructuring assignment [c, ...rest] to extract the first character (stored in c) from the first argument passed into your function. The ...rest takes all characters from the passed in string (excluding the extracted first character) and stores it in the variable rest.

The conditional operator ? : is then used to check if the variable c has a truthy value (ie: a valid character). c will hold a falsy value (undefined in our case) when countChar() is passed an empty string. If c is truthy, then it holds a valid character value, and so the result of (c === target) countChar(rest, target) is used as the return value of the function call.

Here (c === target) will either evaluate to true or false, which when use with countChar(rest, target) converts the boolean value to a numeric value, 1 if it is true, or 0 if it is false. The : 0; means that 0 is used as the return value when c is a falsy value (ie: no more characters are left to check).

  • Related