Advertisement
dimipan80

Division by 3

Nov 8th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function divisionBy3(number) that finds the sum of digits of integer number n
  2. (n > 9) and checks if the sum is divided by 3 without remainder. Write JS program divisionChecker.js
  3. to check a few numbers. The result should be printed on the console
  4. (“the number is divided by 3 without remainder” or “the number is not divided by 3 without remainder”). */
  5.  
  6. "user strict";
  7.  
  8. function divisionBy3(number) {
  9.     if (number > 9) {
  10.         var sum = 0;
  11.         do {
  12.             sum += number % 10;
  13.             number = parseInt(number / 10);
  14.         } while (number);
  15.  
  16.         if (!(sum % 3)) {
  17.             console.log('the number is divided by 3 without remainder');
  18.         } else {
  19.             console.log('the number is not divided by 3 without remainder');
  20.         }
  21.     } else {
  22.         console.error('Error! - The number must been bigger from 9!');
  23.     }
  24. }
  25.  
  26. divisionBy3(6);
  27. divisionBy3(12);
  28. divisionBy3(188);
  29. divisionBy3(591);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement