Advertisement
maramizo

Untitled

Jun 21st, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function calculate(n1, operator, n2) {
  2.  
  3. let result = 0
  4.  
  5. if (operator === '+') {
  6. result = n1 + n2
  7. console.log(result);
  8.  
  9. } else if (operator === '-') {
  10. result = n1 - n2
  11. console.log(result);
  12.  
  13. } else if (operator === '*') {
  14. result = n1 * n2
  15. console.log(result);
  16.  
  17. } else if (operator === '/') {
  18. if (n2 === 0) {
  19. console.log('Cannot divide by zero');
  20.  
  21. } else {
  22. result = n1 / n2
  23. console.log(result)
  24.  
  25. }
  26.  
  27. }
  28.  
  29.  
  30. }
  31.  
  32. //for(starting value; ending condition; increment per loop)
  33. //so in this example, i starts as 3, then increases by 2 after every loop
  34. //until i > 11, which is our ending condition.
  35. for(var i=3;i>11;i+=2)
  36. {
  37. //we're trying to calculate 3/2, 5/4, 7/6, 9/8 and 11/10.
  38. calculate(i/i-1);
  39. }
  40. //this is how simple it is done. what happens is this:
  41. //calculate is called the first time with calculate(3/3-1) which is calculate(3/2)
  42. //i is then incremented by 2, so calculate is called again calculate(5/4), and so on
  43. //until i is larger than 11, and it ends the loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement