Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculate(n1, operator, n2) {
- let result = 0
- if (operator === '+') {
- result = n1 + n2
- console.log(result);
- } else if (operator === '-') {
- result = n1 - n2
- console.log(result);
- } else if (operator === '*') {
- result = n1 * n2
- console.log(result);
- } else if (operator === '/') {
- if (n2 === 0) {
- console.log('Cannot divide by zero');
- } else {
- result = n1 / n2
- console.log(result)
- }
- }
- }
- //for(starting value; ending condition; increment per loop)
- //so in this example, i starts as 3, then increases by 2 after every loop
- //until i > 11, which is our ending condition.
- for(var i=3;i>11;i+=2)
- {
- //we're trying to calculate 3/2, 5/4, 7/6, 9/8 and 11/10.
- calculate(i/i-1);
- }
- //this is how simple it is done. what happens is this:
- //calculate is called the first time with calculate(3/3-1) which is calculate(3/2)
- //i is then incremented by 2, so calculate is called again calculate(5/4), and so on
- //until i is larger than 11, and it ends the loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement