Решения от подписчиков - JavaScript Задача #3

Решения от подписчиков - JavaScript Задача #3



Условие здесь, решение от админа канала здесь.

@phantomhere

const count = prompt('frogs pls!');
       const arr = [];
       for (let i = 0; i < count; i++) {
           let q = prompt().split(' ');
           arr.push(q);
       }

       arr.forEach(item => {
           makeArrInt(item);
           console.log(calcPosition(item))  
       });

       function makeArrInt(arr) {
           for (let i = 0; i < arr.length; i++) {
               arr[i] = +arr[i];   
           }
       }

       function calcPosition(arr) {
           let x = 0;
           const [a, b, k] = arr;
           let k1, k2;

           if (k % 2 == 0) {
               k1 = k2 = k/2;
           }
           else {
               k1 = (k + 1) / 2;
               k2 = (k - 1) / 2;
           }
           
           x += a * k1;
           x -= b * k2;
           
           return x;
       }

Vlad K.

const countOfFrogs = Number.parseInt(prompt('Количество лягух:')) || 0;

let data = [];

for (let i = 0; i < countOfFrogs; i++) {
   let frogData = prompt(`Лягуха (${i}): a,b,k через пробел:`).split(' ');
   if (frogData.length !== 3) continue;
   data.push(frogData);
}
const checkJumps = (a, b, k) => a * Math.ceil(k / 2) - b * Math.floor(k / 2);
data.forEach((frog, index) => {
   console.log(`${index}: ${checkJumps(frog[0], frog[1], frog[2])}`);
});

Danill14301

function input() {
   let k = prompt('Введите число лягушек: '),
       arr = [],
       result = [];

   for (let i = 0; i < k; i++) {
       let xx = prompt(`[${i+1} лягушка] a, b, jumps: `),
           x = xx.split(' ');

       let a = Number(x[0]),
           b = Number(x[1]),
           jumpCount = Number(x[2]);

       arr[i] = [a, b, jumpCount];
   }

   let f = 0;

   for (let i = 0; i < arr.length; i++) {
       result[f] = 0;

       for (let j = 0; j < arr[i][2]; j++) {
           if (j % 2 == 0) result[f] += Number(arr[i][0]);
           else result[f] -= Number(arr[i][1]);
       }

       f++;
   }

   console.log("Result: " + result);
}

input();

Report Page