Here is where is become important to understand the value of functions.
And separating human
thinking from the problem. Instead think of it more like a task list.
In order to perform our task we need:
- A valid operator,
Add
, Subtract
etc.
- 2 valid numbers.
So first off lets get the user to give us a valid operator:
const getValidOperator = () => {
// Lets store our allowed operations in an array.
const allowed = ['add', 'subtract', 'multiply', 'divide'];
const prompt = `Please select an operator from:\n ${allowed.join('\n')}`;
// We will keep looping until we get a valid response.
while(true) {
const res = window.prompt(prompt);
// Using the Array.some() method we can check
// if user entry is in allowed list.
if(allowed.some(a => a === res.toLowerCase())){
return res;
}
}
}
// Just test code.
document.querySelector('p').innerText = getValidOperator();
<p></p>
At this point it is probably starting to seem obvious, we need to have a method for getting a valid number. So lets write that as well.
const getValidNumber = () => {
const prompt = 'Please enter a valid number:';
// We will keep looping until we get a valid response.
while(true) {
const res = window.prompt(prompt);
if(!isNaN(res)) {
return res;
}
}
}
// Just test code.
document.querySelector('p').innerText = getValidNumber();
<p></p>
Now, we need our calculator function. But we already know we have a valid operator. So we do not need to use a switch, instead we can call operators directly.
const calculator = {
add: (p1, p2) => `${p1} + ${p2} = ${p1 + p2}`,
subtract: (p1, p2) => `${p1} - ${p2} = ${p1 - p2}`,
multiply: (p1, p2) => `${p1} * ${p2} = ${p1 * p2}`,
divide: (p1, p2) => `${p1} / ${p2} = ${p1 / p2}`,
};
document.querySelector('p').innerText = calculator['add'](1,2);
<p></p>
Now, let's put all that together:
const getValidOperator = () => {
// Lets store our allowed operations in an array.
const allowed = ['add', 'subtract', 'multiply', 'divide'];
const prompt = `Please select an operator from:\n ${allowed.join('\n')}`;
// We will keep looping until we get a valid response.
while (true) {
const res = window.prompt(prompt);
// Using the Array.some() method we can check
// if user entry is in allowed list.
if (allowed.some(a => a === res.toLowerCase())) {
return res;
}
}
}
const getValidNumber = () => {
const prompt = 'Please enter a valid number:';
// We will keep looping until we get a valid response.
while (true) {
const res = window.prompt(prompt);
if (!isNaN(res)) {
return res;
}
}
}
const calculator = {
add: (p1, p2) => `${p1} + ${p2} = ${p1 + p2}`,
subtract: (p1, p2) => `${p1} - ${p2} = ${p1 - p2}`,
multiply: (p1, p2) => `${p1} * ${p2} = ${p1 * p2}`,
divide: (p1, p2) => `${p1} / ${p2} = ${p1 / p2}`,
};
document.querySelector('#doCalc').addEventListener('click', () => {
const num1 = getValidNumber();
const num2 = getValidNumber();
const op = getValidOperator();
document.querySelector('p').innerText = calculator[op](num1, num2);
});
<h1>Calculator</h1>
<p></p>
<button id="doCalc">Click to Start</button>
From here we can take the next step.
Lets make it extensible, so that if we add a new operator it just works.
Instead of having a fixed array
of allowed operators, lets reflect
over our calculator using Object.keys()
. Using this technique we can add a new method to calculator
and it magically becomes available.
const getValidOperator = (calculator) => {
const allowed = Object.keys(calculator);
const prompt = `Please select an operator from:\n ${allowed.join('\n')}`;
// We will keep looping until we get a valid response.
while (true) {
const res = window.prompt(prompt);
// Using the Array.some() method we can check
// if user entry is in allowed list.
if (allowed.some(a => a === res.toLowerCase())) {
return res;
}
}
}
const getValidNumber = () => {
const prompt = 'Please enter a valid number:';
// We will keep looping until we get a valid response.
while (true) {
const res = window.prompt(prompt);
if (!isNaN(res)) {
return res;
}
}
}
const calculator = {
add: (p1, p2) => `${p1} + ${p2} = ${p1 + p2}`,
subtract: (p1, p2) => `${p1} - ${p2} = ${p1 - p2}`,
multiply: (p1, p2) => `${p1} * ${p2} = ${p1 * p2}`,
divide: (p1, p2) => `${p1} / ${p2} = ${p1 / p2}`,
mod: (p1, p2) => `${p1} % ${p2} = ${p1 % p2}`
};
document.querySelector('#doCalc').addEventListener('click', () => {
const num1 = getValidNumber();
const num2 = getValidNumber();
const op = getValidOperator(calculator);
document.querySelector('p').innerText = calculator[op](num1, num2);
});
<h1>Calculator</h1>
<p></p>
<button id="doCalc">Click to Start</button>