constcalculator=operation=>{// verify that the string with the operation has the right format:// - a number// - optionally followed by 0 or more:// - a space// - an operator (+-*/)// - a space// - another numberif(!operation.match(/^\d+([\+\-\/\*]\d+)*$/))returnnull;// easy solution: now that we know the string has the format that we expect, // return (eval(operation));// ...but someone did it already, so let's go the long way :P// break the string by spacesletops=operation.split("");letopsSimple=[];// multiplication and division take priority// we create a new array that will only have numbers and + or -for(x=0;x<ops.length;x++){if(ops[x]==='*'){constval=opsSimple.pop();opsSimple.push(val*ops[x+1]);x++;}elseif(ops[x]==='/'){// do not allow division by zero!if(ops[x+1]==="0")return"Error! Division by zero!";constval=opsSimple.pop();opsSimple.push(val/ops[x+1]);x++;}else{opsSimple.push(ops[x]);}}// calculate the addtiions and substractions sequentiallyletresult=parseInt(opsSimple[0]);for(x=1;x<opsSimple.length;x=x+2){if(opsSimple[x]==='+'){result+=opsSimple[x+1];}else{result-=opsSimple[x+1];}}returnresult;}
JavaScript
Live demo on CodePen
10 out of 10 challenges! :)
Although I was just able to make 2 using CSS only :-/
I'm gonna try to keep doing em each day in July if I can, you game lol?
I'm currently a day behind you since I didn't get this one done yesterday
Let's do it!