Advertisement
Spocoman

03. Division

Jan 13th, 2022
81
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function division(num) {
  2.  
  3.     let divisor = 0;
  4.     if (num % 10 === 0) {
  5.         divisor = 10;
  6.     } else if (num % 7 === 0) {
  7.         divisor = 7;
  8.     } else if (num % 6 === 0) {
  9.         divisor = 6;
  10.     } else if (num % 3 === 0) {
  11.         divisor = 3;
  12.     } else if (num % 2 === 0) {
  13.         divisor = 2;
  14.     } else {
  15.         console.log('Not divisible');
  16.     }
  17.    
  18.     if (divisor !== 0){
  19.         console.log(`The number is divisible by ${divisor}`);
  20.     }
  21.    
  22. }
  23.  
  24.  
  25. Решение с тернарен оператор:
  26.  
  27. function division(num) {
  28.  
  29.     let divisor =
  30.         num % 10 === 0? 10:
  31.             num % 7 === 0? 7:
  32.                 num % 6 === 0? 6:
  33.                     num % 3 === 0? 3:
  34.                         num % 2 === 0? 2 : 'Not divisible';
  35.  
  36.     console.log(divisor !== 'Not divisible'? `The number is divisible by ${divisor}`: divisor);
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement