| 3 | 3 | // convenience to get you started writing code faster. |
| 4 | 4 | // |
| 5 | 5 | |
| 6 | | export const answer = () => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 6 | export const answer = (question) => { |
| 7 | // Edge Case: Empty or whitespace-only input |
| 8 | if (!question || question.trim() === '') { |
| 9 | throw new Error('Syntax error'); |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Malformed syntax with missing operands |
| 13 | if (question === 'What is?') { |
| 14 | throw new Error('Syntax error'); |
| 15 | } |
| 16 | |
| 17 | // Edge Case: Non-math questions |
| 18 | if (!question.startsWith('What is ')) { |
| 19 | throw new Error('Unknown operation'); |
| 20 | } |
| 21 | |
| 22 | // Remove the "What is " prefix and the trailing "?" |
| 23 | let expression = question.substring(8, question.length - 1).trim(); |
| 24 | |
| 25 | // Edge Case: Empty expression after removing prefix and suffix |
| 26 | if (expression === '') { |
| 27 | throw new Error('Syntax error'); |
| 28 | } |
| 29 | |
| 30 | // Tokenize the expression |
| 31 | const tokens = expression.split(' '); |
| 32 | |
| 33 | // Edge Case: Invalid syntax with consecutive operators |
| 34 | if (tokens.length === 0) { |
| 35 | throw new Error('Syntax error'); |
| 36 | } |
| 37 | |
| 38 | // Parse the first number |
| 39 | let result = parseInt(tokens[0]); |
| 40 | |
| 41 | // Edge Case: First token is not a valid number |
| 42 | if (isNaN(result)) { |
| 43 | throw new Error('Syntax error'); |
| 44 | } |
| 45 | |
| 46 | // Process the rest of the tokens |
| 47 | for (let i = 1; i < tokens.length; i++) { |
| 48 | const operator = tokens[i]; |
| 49 | |
| 50 | // Edge Case: Check if we have a number when expecting an operator |
| 51 | if (!isNaN(parseInt(operator)) && operator !== '-') { |
| 52 | throw new Error('Syntax error'); |
| 53 | } |
| 54 | |
| 55 | // Perform the operation |
| 56 | switch (operator) { |
| 57 | case 'plus': |
| 58 | // Edge Case: Check if operand exists |
| 59 | if (i + 1 >= tokens.length) { |
| 60 | throw new Error('Syntax error'); |
| 61 | } |
| 62 | const plusOperand = parseInt(tokens[i + 1]); |
| 63 | // Edge Case: Operand is not a valid number |
| 64 | if (isNaN(plusOperand)) { |
| 65 | throw new Error('Syntax error'); |
| 66 | } |
| 67 | result += plusOperand; |
| 68 | i += 1; // Skip the operand |
| 69 | break; |
| 70 | case 'minus': |
| 71 | // Edge Case: Check if operand exists |
| 72 | if (i + 1 >= tokens.length) { |
| 73 | throw new Error('Syntax error'); |
| 74 | } |
| 75 | const minusOperand = parseInt(tokens[i + 1]); |
| 76 | // Edge Case: Operand is not a valid number |
| 77 | if (isNaN(minusOperand)) { |
| 78 | throw new Error('Syntax error'); |
| 79 | } |
| 80 | result -= minusOperand; |
| 81 | i += 1; // Skip the operand |
| 82 | break; |
| 83 | case 'multiplied': |
| 84 | // Edge Case: Check if next token is "by" for "multiplied by" |
| 85 | if (i + 2 >= tokens.length || tokens[i + 1] !== 'by') { |
| 86 | throw new Error('Syntax error'); |
| 87 | } |
| 88 | const multOperand = parseInt(tokens[i + 2]); |
| 89 | // Edge Case: Operand is not a valid number |
| 90 | if (isNaN(multOperand)) { |
| 91 | throw new Error('Syntax error'); |
| 92 | } |
| 93 | result *= multOperand; |
| 94 | i += 2; // Skip both "by" and the operand |
| 95 | break; |
| 96 | case 'divided': |
| 97 | // Edge Case: Check if next token is "by" for "divided by" |
| 98 | if (i + 2 >= tokens.length || tokens[i + 1] !== 'by') { |
| 99 | throw new Error('Syntax error'); |
| 100 | } |
| 101 | const divOperand = parseInt(tokens[i + 2]); |
| 102 | // Edge Case: Operand is not a valid number |
| 103 | if (isNaN(divOperand)) { |
| 104 | throw new Error('Syntax error'); |
| 105 | } |
| 106 | // Edge Case: Division by zero |
| 107 | if (divOperand === 0) { |
| 108 | throw new Error('Syntax error'); |
| 109 | } |
| 110 | result /= divOperand; |
| 111 | i += 2; // Skip both "by" and the operand |
| 112 | break; |
| 113 | default: |
| 114 | // Edge Case: Unsupported operations |
| 115 | throw new Error('Unknown operation'); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return result; |
| 120 | // Handled Edge Cases: Empty input, whitespace-only input, non-math questions, missing operands, |
| 121 | // invalid syntax with consecutive operators, first token not a number, |
| 122 | // incomplete expression, operand not a number, unsupported operations, |
| 123 | // missing "by" in multiplied/divided operations, division by zero |
| 8 | 124 | }; |