TZinovieva

Equal Sums Even Odd Position JS

Oct 28th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function equalSumsEvenOddPosition(input) {
  2.     let start = Number(input[0]);
  3.     let end = Number(input[1]);
  4.  
  5.     let buff = "";
  6.     for (let currentNumber = start; currentNumber <= end; currentNumber++) {
  7.         let currNumString = currentNumber.toString();
  8.  
  9.         let evenSum = 0;
  10.         let oddSum = 0;
  11.  
  12.         for (let index = 0; index < currNumString.length; index++) {
  13.            let currentDigit = Number(currNumString[index]);
  14.            let position = index + 1;
  15.  
  16.             if (position % 2 === 0) {
  17.                 evenSum += currentDigit;
  18.             } else {
  19.                 oddSum += currentDigit;
  20.             }
  21.         }
  22.             if (evenSum === oddSum) {
  23.             buff += `${currNumString} `
  24.             }
  25.     }  
  26.     console.log(buff);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment