Advertisement
drkskwlkr

Operations with Numbers

Jul 20th, 2022
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calc(input) {
  2.  
  3.   let n1 = Number(input[0]) ;
  4.   let n2 = Number(input[1]) ;
  5.   let op = input[2] ;
  6.  
  7.   let result = 0 ;
  8.   let remainder = "" ;
  9.  
  10.   switch (op) {
  11.  
  12.     case "+" :
  13.       result = n1 + n2 ;
  14.       if ( result % 2 === 0) {
  15.         console.log(`${n1} ${op} ${n2} = ${result} - even`) ;
  16.       } else {
  17.         console.log(`${n1} ${op} ${n2} = ${result} - odd`) ;
  18.       }
  19.       break ;
  20.  
  21.     case "-" :
  22.       result = n1 - n2 ;
  23.       if ( result % 2 === 0) {
  24.         console.log(`${n1} ${op} ${n2} = ${result} - even`) ;
  25.       } else {
  26.         console.log(`${n1} ${op} ${n2} = ${result} - odd`) ;
  27.       }
  28.       break ;
  29.  
  30.     case "*" :
  31.       result = n1 * n2 ;
  32.       if ( result % 2 === 0) {
  33.         console.log(`${n1} ${op} ${n2} = ${result} - even`) ;
  34.       } else {
  35.         console.log(`${n1} ${op} ${n2} = ${result} - odd`) ;
  36.       }
  37.       break ;
  38.  
  39.     case "/" :
  40.       if (n2 === 0) {
  41.         console.log(`Cannot divide ${n1} by zero`)
  42.       } else {
  43.         result = n1 / n2 ;
  44.         console.log(`${n1} / ${n2} = ${result.toFixed(2)}`)
  45.       } ;
  46.       break ;
  47.  
  48.     case "%" :
  49.       if (n2 === 0) {
  50.         console.log(`Cannot divide ${n1} by zero`)
  51.       } else {
  52.         result = n1 % n2 ;
  53.         console.log(`${n1} % ${n2} = ${result}`) ;
  54.       } ;
  55.       break ;
  56.   }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement