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 + 1→a += 1a = a - 1→a -= 1a = a * 2→a *= 2a = a / 1→a /= 1a = 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.
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 + 1→a += 1a = a - 1→a -= 1a = a * 2→a *= 2a = a / 1→a /= 1a = 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.
Your exercise will be checked with these tests:
import { expect, test } from "vitest";
import f from "./index.js";
test("syntactic sugar", () => {
const text = "If I look back I am lost";
expect(f(text, "I")).toEqual("f look back am lost");
expect(f("zz Zorro", "z")).toEqual(" Zorro");
});Teacher's solution will be available in:
20:00
