Lesson 2 of 15

Tokenizing Full Arithmetic

Expanding the Tokenizer

Our tokenizer handles + and -. Now let's add multiplication and division:

TypeMatches
STAR*
SLASH/
LPAREN(
RPAREN)

With these additions, we can tokenize any arithmetic expression like (3 + 4) * 2.

Handling Parentheses

Parentheses are single-character tokens, just like operators:

if (input[i] === "(") {
    tokens.push({ type: "LPAREN", value: "(" });
    i++;
}

Your Task

Extend the tokenizer to handle all six operators/delimiters: +, -, *, /, (, ), plus numbers and whitespace. Return the same { type, value } format.

Node.js loading...
Loading...
Click "Run" to execute your code.