ALSAVOV

Untitled

Jun 21st, 2023 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 4.43 KB | Source Code | 0 0
  1. function heartDeliv(data) {
  2.     let hearts = data.shift().split("@").map(Number); // [2, 4, 2]
  3.     let heartsLength = hearts.length;
  4.  
  5.     let lastPositionIndex = 0;
  6.  
  7.     let currentIndex = 0;
  8.     let index = 0;
  9.  
  10.     let command = data[index];
  11.     index++;
  12.  
  13.     while (command !== "Love!") {
  14.         let tokens = command.split(" "); // ["Jump", "2"]
  15.         let currentLength = +tokens[1]; // взимам единствено текущата дължина на скока като число
  16.  
  17.         currentIndex += currentLength; // прибавям текущата дължина на скока към текущия индекс
  18.  
  19.         // проверявам дали текущият индекс е в границите на масива hearts, т.е дали е валиден
  20.         if (currentIndex <= heartsLength - 1) {
  21.             // проверявам дали стойността на елемента с текущия индекс вече е = на 0
  22.             if (hearts[currentIndex] === 0) {
  23.                 console.log(
  24.                     `Place ${currentIndex} already had Valentine's day.`
  25.                );
  26.                // вземам следващия скок
  27.                command = data[index];
  28.                index++;
  29.                continue;
  30.            } else {
  31.                // ако стойността на елемента с текущия индекс не е равна на 0, изваждам -2 от стойността
  32.                hearts[currentIndex] -= 2;
  33.            }
  34.        } else {
  35.            // ако текущият индекс Не е в рамките на масива, текущия индекс става равен на 0
  36.            currentIndex = 0;
  37.            // проверявам дали стойността на елемента с текущия индекс вече е = на 0
  38.            if (hearts[currentIndex] === 0) {
  39.                console.log(
  40.                    `Place ${currentIndex} already had Valentine's day.`
  41.                 );
  42.                 // вземам следващия скок
  43.                 command = data[index];
  44.                 index++;
  45.                 continue;
  46.             } else {
  47.                 // ако стойността на елемента с текущия индекс не е равна на 0, изваждам -2 от стойността
  48.                 hearts[currentIndex] -= 2;
  49.             }
  50.         }
  51.  
  52.         // проверявам дали стойността на елемента (след като съм извадил от стойността на елемента 2) с
  53.         // текущия индекс е по-малка от 0 и ако Е, тогава ѝ задавам стойност 0.
  54.         if (hearts[currentIndex] < 0) {
  55.             hearts[currentIndex] = 0;
  56.             console.log(`Place ${currentIndex} has Valentine's day.`);
  57.        } else if (hearts[currentIndex] === 0) {
  58.            console.log(`Place ${currentIndex} has Valentine's day.`);
  59.         }
  60.  
  61.         lastPositionIndex = currentIndex;
  62.  
  63.         command = data[index];
  64.         index++;
  65.     }
  66.  
  67.     console.log(`Cupid's last position was ${lastPositionIndex}.`);
  68.  
  69.    let sum = 0;
  70.    let isSuccessfull = true;
  71.    let failCounter = 0;
  72.  
  73.    // тук вече сме обработили масива hearts и преминавам през него, за да разбера, дали има елементи
  74.    // в него със стойност различна от 0, което означава, че мисията НЕ е успешна; за всяка неуспешна мисия
  75.    // брояча увеличава стойността си с 1, съответно ако мисията е неуспешна променливата isSuccessfull
  76.    // става равна на false. Ако стойността на sum е 0, това значи, че всички елементи са със ст-ст 0,
  77.    // което означава, че мисията е успешна.
  78.    for (let house of hearts) {
  79.        if (house !== 0) {
  80.            failCounter++;
  81.            isSuccessfull = false;
  82.        }
  83.        sum += house;
  84.    }
  85.  
  86.    if (sum === 0) {
  87.        isSuccessfull = true;
  88.    }
  89.  
  90.    if (isSuccessfull) {
  91.        console.log("Mission was successful.");
  92.    } else {
  93.        console.log(`Cupid has failed ${failCounter} places.`);
  94.    }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment