alenblack7

Total Lucky Number

Sep 27th, 2021 (edited)
320
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculateLuckyTicket () {
  2.   let numberOfTicket = "";
  3.   let totalLuckyNumbers = 0;
  4.  
  5.   for( let i = 1; i < 1000000; i++ ){
  6.     numberOfTicket = formatingString(i);
  7.     if(checkLuck(numberOfTicket)){
  8.       console.log(`Счастливое число: ${numberOfTicket}`);
  9.       totalLuckyNumbers++;
  10.     }
  11.   }
  12.   return totalLuckyNumbers;
  13. }
  14.  
  15. function checkLuck (str) {
  16.   let num1 = +str[0] + +str[1] + +str[2];
  17.   let num2 = +str[3] + +str[4] + +str[5];
  18.   if (num1 == num2){
  19.     return true;
  20.   } else {
  21.     return false;
  22.   }
  23. }
  24.  
  25. function formatingString(num) {
  26.   switch(`${num}`.length) {
  27.     case 1:
  28.       return "00000" + num;
  29.     case 2:
  30.       return "0000" + num;
  31.     case 3:
  32.       return "000" + num;
  33.     case 4:
  34.       return "00" + num;
  35.     case 5:
  36.       return "0" + num;
  37.     case 6:
  38.       return "" + num;
  39.   }
  40. }
  41.  
  42. console.log(calculateLuckyTicket());
RAW Paste Data