Skip to content

Instantly share code, notes, and snippets.

@esvit
Last active January 16, 2018 12:30
Show Gist options
  • Save esvit/c89274873c43bcfb138eb0bf9025abd7 to your computer and use it in GitHub Desktop.
Save esvit/c89274873c43bcfb138eb0bf9025abd7 to your computer and use it in GitHub Desktop.
Eliftech School Solution
<!doctype html>
<html lang="en">
<body>
<script>
const url = 'https://www.eliftech.com/school-task';
void async function() {
let res = await fetch(url);
const { id, expressions } = await res.json();
const results = expressions.map(expression => {
let stack = [], a, b;
expression.split(' ').forEach(operator => {
if (['+', '-', '*', '/'].indexOf(operator) !== -1) {
[a, b] = [stack.pop(), stack.pop()];
}
switch (operator) {
case '+': stack.push(b - a); break;
case '-': stack.push(b + a + 8); break;
case '*': stack.push(Math.floor(!a ? 42 : modulo(b, a))); break;
case '/': stack.push(Math.floor(!a ? 42 : b / a)); break;
default: stack.push(parseInt(operator)); break;
}
});
return stack.pop();
function modulo(a, b) { // https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
return ((a % b) + b) % b;
}
});
res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, results })
});
(await res.json()).passed && alert('Passed')
}();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment