Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
2.4 kB
3
Indexable
Never
// tova sa ti vhodnite danni - masiv ot chisla
let inputNumbers = [186, 144, 782, 764, 882, 909, 511]; // ne znam v tqhnata sistema dali ne trqbva da e let inputNumbers = gets()

//tova ti e rezultata, outputa - purvonachalno e prazen posle shte go pulnim s chisla
let alphaNumbers = [];

//minavame prez vsichki chisla ot masiva
for (i = 0; i < inputNumbers.length; i++) {
  //obrushtame chisloto v string za da mojem da dostupvame 3te mu cifri
  let numberAsString = inputNumbers[i].toString();

  // vzimame i zapisvame vsqka cifrichka po-otdelno, plusa otpred e za da gi oburnem obratno v chisla
  let digitOne = +numberAsString[0];
  let digitTwo = +numberAsString[1];
  let digitThree = +numberAsString[2];

  //tuka smqtame sumata
  const sumOfDigits = digitOne + digitTwo + digitThree;

  //tuka vzimame poslednata cifra ot sumata - kato pravim modulno delenie na 10 (t.e. delim na deset i vzimame ostatuka)
  const lastDigitOfSum = sumOfDigits % 10;

  //Sega tuka suzdavame masiv s vsichki vuzmojni razliki, chislata po uslovie sa vse 3 cifreni
  let allPosibleDifferences = [
    digitOne - digitTwo, // Difference between the first and second digits
    digitOne - digitThree, // Difference between the first and third digits
    digitTwo - digitThree, // Difference between the second and third digits
    digitTwo - digitOne, // Difference between the second and first digits
    digitThree - digitOne, // Difference between the third and first digits
    digitThree - digitTwo, // Difference between the third and second digits
  ];

  const biggestDifference = Math.max(...allPosibleDifferences); //Math.max e funkciq koqto i davash masiv ot chisla kato parametyr a tq ti vrushta nai-golqmoto chislo

  // posledna proverka - proverqvame dali chisloto e aplha,
  // toest nai-golqmata vuzmojna razlika mejdu cifrite mu dali e po-golqma ot poslednata cifra na sumata ot cifrite
  const isAlphaNumber = biggestDifference > lastDigitOfSum;

  // sega znaem dali e alpha number ili ne i ako e iskame da go pushnem/vkarame v masiva koito syzdadohme nai-otgore na zadachata
  if (isAlphaNumber) {
    alphaNumbers.push(inputNumbers[i]);
  }
}

//sled kato e minal for cikala v alphaNumbers veche sa vkarani vsichki alpha chisla i prosto e neobhodimo da gi outputnem,
//ne pomnq v sistemata im kak tochno stavashe tova
console.log(alphaNumbers);
Leave a Comment