| 7 | 7 | * @param {string} question |
| 8 | 8 | * @return {number} |
| 9 | 9 | */ |
| 10 | | export function answer(question) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 10 | function answer(question) { |
| 11 | // Edge Case: Empty or malformed input strings |
| 12 | if (!question || typeof question !== 'string') { |
| 13 | throw new Error('Syntax error'); |
| 14 | } |
| 15 | |
| 16 | // Edge Case: Non-math questions |
| 17 | if (!question.startsWith('What is') || !question.endsWith('?')) { |
| 18 | throw new Error('Unknown operation'); |
| 19 | } |
| 20 | |
| 21 | // Remove "What is" and "?" from the question |
| 22 | let expression = question.slice(8, -1).trim(); |
| 23 | |
| 24 | // Edge Case: Empty expression after removing "What is" and "?" |
| 25 | if (!expression) { |
| 26 | throw new Error('Syntax error'); |
| 27 | } |
| 28 | |
| 29 | // Tokenize the expression |
| 30 | const tokens = expression.split(' '); |
| 31 | |
| 32 | // Edge Case: Invalid syntax with consecutive operators or starting/ending with operator |
| 33 | if (tokens.length === 0) { |
| 34 | throw new Error('Syntax error'); |
| 35 | } |
| 36 | |
| 37 | // Parse the first number |
| 38 | let result = parseInt(tokens[0]); |
| 39 | |
| 40 | // Edge Case: First token is not a valid number |
| 41 | if (isNaN(result)) { |
| 42 | throw new Error('Syntax error'); |
| 43 | } |
| 44 | |
| 45 | // Process operations from left to right |
| 46 | for (let i = 1; i < tokens.length;) { |
| 47 | const operator = tokens[i]; |
| 48 | |
| 49 | // Check if operator is valid |
| 50 | const validOperators = ['plus', 'minus', 'multiplied', 'divided']; |
| 51 | if (!validOperators.includes(operator)) { |
| 52 | throw new Error('Unknown operation'); |
| 53 | } |
| 54 | |
| 55 | // Check if we have enough tokens for a complete operation |
| 56 | if (i + 1 >= tokens.length) { |
| 57 | throw new Error('Syntax error'); |
| 58 | } |
| 59 | |
| 60 | // Handle two-word operators |
| 61 | if (operator === 'multiplied' || operator === 'divided') { |
| 62 | // Edge Case: Must be followed by "by" |
| 63 | if (i + 1 >= tokens.length || tokens[i + 1] !== 'by') { |
| 64 | throw new Error('Syntax error'); |
| 65 | } |
| 66 | // Edge Case: Must have operand after "by" |
| 67 | if (i + 2 >= tokens.length) { |
| 68 | throw new Error('Syntax error'); |
| 69 | } |
| 70 | |
| 71 | const operand = parseInt(tokens[i + 2]); |
| 72 | // Edge Case: Operand is not a valid number |
| 73 | if (isNaN(operand)) { |
| 74 | throw new Error('Syntax error'); |
| 75 | } |
| 76 | |
| 77 | if (operator === 'multiplied') { |
| 78 | result *= operand; |
| 79 | } else if (operator === 'divided') { |
| 80 | // Edge Case: Division by zero |
| 81 | if (operand === 0) { |
| 82 | throw new Error('Division by zero'); |
| 83 | } |
| 84 | result /= operand; |
| 85 | } |
| 86 | |
| 87 | i += 3; // Skip operator, "by", and operand |
| 88 | } else { |
| 89 | // Handle single-word operators |
| 90 | const operand = parseInt(tokens[i + 1]); |
| 91 | // Edge Case: If the next token is not a number |
| 92 | if (isNaN(operand)) { |
| 93 | // If the next token is a word, it's a syntax error (consecutive operators) |
| 94 | if (/^[a-zA-Z]+$/.test(tokens[i + 1])) { |
| 95 | throw new Error('Syntax error'); |
| 96 | } else { |
| 97 | throw new Error('Syntax error'); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | switch (operator) { |
| 102 | case 'plus': |
| 103 | result += operand; |
| 104 | break; |
| 105 | case 'minus': |
| 106 | result -= operand; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | i += 2; // Skip operator and operand |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return result; |
| 12 | 115 | }; |
| 116 | |
| 117 | module.exports = { answer }; |