0

Всем привет, начал изучать js и добрался до функций. Есть такая задачка. Я застрял на 3 задании и никак не могу догнать как это сделать... Объясните, пожалуйста

const btn = document.getElementById("play");
const player1 = document.getElementById("player1");
const player2 = document.getElementById("player2");




function getPlayerResult() {
let result = Math.floor((Math.random() * 3) + 1);

let num1 = result;
let num2 = result;



function getElemById() {

    if (result == 1) {
        result = 'Камень';
    } else if (result == 2) {
        result = 'Ножницы';
    } else if (result == 3) {
        result = 'Бумага';
    } 

 }

function determineWinner(count1, count2) {

}


getElemById();
determineWinner(num1, num2);

return result;
}


function runGame() {

player1.innerHTML = getPlayerResult();
player2.innerHTML = getPlayerResult();


}



btn.addEventListener("click", runGame);

введите сюда описание изображения

1
  • Тебе нужно возвращать значений из функций, что бы потом можно было использовать в других функциях.
    – singlesly
    6 мая 2020 в 11:17

1 ответ 1

0

Отнимаем из числа второго игрока число первого. Видим закономерность, что если difference = 1, либо difference = -2, побеждает первый игрок, что соответствует реальности. (По сути можно не только отнимать, просто другие значения будут)

function determineWinner(player_1, player_2) {
    var difference = player_2 - player_1;

    if (difference == 1 || difference == -2) {
        return 1; // победил первый игрок
    } else if (difference == 0){
        return 0; // Ничья
    } else {
        return 2; // победил второй игрок
    };
};

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.