Logo

JavaScript: Syntactic sugar

Expressions like index = index + 1 occur quite often in JavaScript, so the creators of the language added a shortcut: index += 1. This shortcut is usually referred to as syntactic sugar because it simplifies and "sweetens" the process of coding :)

There are shortcuts for all arithmetic operations and for string concatenation:

  • a = a + 1a += 1
  • a = a - 1a -= 1
  • a = a * 2a *= 2
  • a = a / 1a /= 1
  • a = a + 'foo'a += 'foo'

Instructions

Write the filterString() function that takes a string and a character as input and returns a new string, from which all occurrences of the character are removed.

const str = "If I look back I am lost";
filterString(str, "I"); // 'f  look back  am lost'
filterString("zz Zorro", "z"); // ' Zorro'

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!