How can I make the game Rock, Paper, Scissors move forward between rounds using the DOM in javascript?

I hope you can help me. I am making the game Rock, Paper, Scissors in JavaScript, and I am stuck. The problem is that I can’t make a loop work to allow me to play 5 rounds in the game, keep a round counter, and update the score. The game is for practicing DOM manipulation.

In the end, I would expect the program to give me the final score and a message to the user to notify if they have won or lost.

I am sharing my complete code with you, thank you very much for your help!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>let humanScore = 0
let computerScore = 0
let rounds = 0;
const getComputerChoice = ()=> {
const items = ["rock", "paper", "scissors"];
const randomItems = items[(Math.floor(Math.random() * items.length))];
return randomItems;
}
const getHumanChoice = ()=> {
return new Promise((resolve) => {
const rockSelection = document.querySelector("#rock");
const paperSelection = document.querySelector("#paper");
const scissorsSelection = document.querySelector("#scissors");
rockSelection.addEventListener("click", ()=> {
rockSelection.setAttribute("class", "animation");
resolve("rock"), {once: true};
});
paperSelection.addEventListener("click", ()=> {
paperSelection.setAttribute("class", "animation");
resolve("paper"), {once: true};
});
scissorsSelection.addEventListener("click", ()=> {
scissorsSelection.setAttribute("class", "animation");
resolve("scissors"), {once: true};
});
});
}
const playRound = (humanChoice, computerChoice)=> {
const textResponse = document.querySelector("#display");
const para = document.createElement("p");
para.classList.add("para");
para.textContent = "";
textResponse.appendChild(para);
if(humanChoice == "rock" && computerChoice == "scissors") {
para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ✂️`;
humanScore++;
} else if (humanChoice == "scissors" && computerChoice == "paper") {
para.textContent = `You win! ${humanChoice} ✂️ beats ${computerChoice} ????`;
humanScore++;
} else if (humanChoice == "paper" && computerChoice == "rock") {
para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ????`;
humanScore++;
} else if (humanChoice === computerChoice) {
para.textContent = `You draw! ????. The computer choose ${computerChoice} too!`;
} else {
para.textContent = `You lose! ????. The computer choose ${computerChoice}`;
computerScore++;
}
}
const playGame = async ()=> {
const humanChoice = await getHumanChoice();
const computerChoice = getComputerChoice();
playRound(humanChoice, computerChoice);
const score = document.querySelector("#score");
const newPara = document.createElement("p");
newPara.classList.add("Score");
newPara.textContent =`ROUND ${rounds}: You ${humanScore},
The computer: ${computerScore}`;
score.appendChild(newPara);
if (rounds < 5) {
const nextDiv = document.createElement("div");
const nextRound = document.createElement("button");
nextDiv.classList.add("nextDiv");
nextRound.classList.add("button");
nextRound.textContent = "Next round";
newPara.appendChild(nextDiv);
nextDiv.appendChild(nextRound);
return new Promise((resolve) => {
nextRound.addEventListener("click", () => {
resolve();
}, { once: true });
});
}
};
const loopRounds = async ()=> {
while (rounds < 5) {
rounds++;
await playGame()
}
const finalMessage = document.createElement("p");
if (humanScore > computerScore) {
finalMessage.textContent = "You are the best!";
} else if ( humanScore < computerScore) {
finalMessage.textContent = "You are the worst in the world!";
} else {
finalMessage.textContent = "It's a draw!"
}
document.querySelector("#score").appendChild(finalMessage);
}
loopRounds();
</code>
<code>let humanScore = 0 let computerScore = 0 let rounds = 0; const getComputerChoice = ()=> { const items = ["rock", "paper", "scissors"]; const randomItems = items[(Math.floor(Math.random() * items.length))]; return randomItems; } const getHumanChoice = ()=> { return new Promise((resolve) => { const rockSelection = document.querySelector("#rock"); const paperSelection = document.querySelector("#paper"); const scissorsSelection = document.querySelector("#scissors"); rockSelection.addEventListener("click", ()=> { rockSelection.setAttribute("class", "animation"); resolve("rock"), {once: true}; }); paperSelection.addEventListener("click", ()=> { paperSelection.setAttribute("class", "animation"); resolve("paper"), {once: true}; }); scissorsSelection.addEventListener("click", ()=> { scissorsSelection.setAttribute("class", "animation"); resolve("scissors"), {once: true}; }); }); } const playRound = (humanChoice, computerChoice)=> { const textResponse = document.querySelector("#display"); const para = document.createElement("p"); para.classList.add("para"); para.textContent = ""; textResponse.appendChild(para); if(humanChoice == "rock" && computerChoice == "scissors") { para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ✂️`; humanScore++; } else if (humanChoice == "scissors" && computerChoice == "paper") { para.textContent = `You win! ${humanChoice} ✂️ beats ${computerChoice} ????`; humanScore++; } else if (humanChoice == "paper" && computerChoice == "rock") { para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ????`; humanScore++; } else if (humanChoice === computerChoice) { para.textContent = `You draw! ????. The computer choose ${computerChoice} too!`; } else { para.textContent = `You lose! ????. The computer choose ${computerChoice}`; computerScore++; } } const playGame = async ()=> { const humanChoice = await getHumanChoice(); const computerChoice = getComputerChoice(); playRound(humanChoice, computerChoice); const score = document.querySelector("#score"); const newPara = document.createElement("p"); newPara.classList.add("Score"); newPara.textContent =`ROUND ${rounds}: You ${humanScore}, The computer: ${computerScore}`; score.appendChild(newPara); if (rounds < 5) { const nextDiv = document.createElement("div"); const nextRound = document.createElement("button"); nextDiv.classList.add("nextDiv"); nextRound.classList.add("button"); nextRound.textContent = "Next round"; newPara.appendChild(nextDiv); nextDiv.appendChild(nextRound); return new Promise((resolve) => { nextRound.addEventListener("click", () => { resolve(); }, { once: true }); }); } }; const loopRounds = async ()=> { while (rounds < 5) { rounds++; await playGame() } const finalMessage = document.createElement("p"); if (humanScore > computerScore) { finalMessage.textContent = "You are the best!"; } else if ( humanScore < computerScore) { finalMessage.textContent = "You are the worst in the world!"; } else { finalMessage.textContent = "It's a draw!" } document.querySelector("#score").appendChild(finalMessage); } loopRounds(); </code>
let humanScore = 0
let computerScore = 0
let rounds = 0;

const getComputerChoice = ()=> {
    const items = ["rock", "paper", "scissors"];
    const randomItems = items[(Math.floor(Math.random() * items.length))];
    return randomItems;
}

const getHumanChoice = ()=> {
    return new Promise((resolve) => {
        const rockSelection = document.querySelector("#rock");
        const paperSelection = document.querySelector("#paper");
        const scissorsSelection = document.querySelector("#scissors");
    
        rockSelection.addEventListener("click", ()=> {
            rockSelection.setAttribute("class", "animation");
           resolve("rock"), {once: true};
        });
    
        paperSelection.addEventListener("click", ()=> {
            paperSelection.setAttribute("class", "animation");
            resolve("paper"), {once: true};
        });
    
        scissorsSelection.addEventListener("click", ()=> {
            scissorsSelection.setAttribute("class", "animation");
            resolve("scissors"), {once: true};
        });
    });
}


const playRound = (humanChoice, computerChoice)=> {
    const textResponse = document.querySelector("#display");
    const para = document.createElement("p");
    para.classList.add("para");
    para.textContent = "";
    textResponse.appendChild(para);

    if(humanChoice == "rock" && computerChoice == "scissors") {
        para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ✂️`;
        humanScore++;
    } else if (humanChoice == "scissors" && computerChoice == "paper") {
        para.textContent = `You win! ${humanChoice} ✂️ beats ${computerChoice} ????`;
        humanScore++;
    } else if (humanChoice == "paper" && computerChoice == "rock") {
        para.textContent = `You win! ${humanChoice} ???? beats ${computerChoice} ????`;
        humanScore++;
    } else if (humanChoice === computerChoice) {
        para.textContent = `You draw! ????. The computer choose ${computerChoice} too!`;
    } else {
        para.textContent = `You lose! ????. The computer choose ${computerChoice}`;
        computerScore++;
    }
}

const playGame = async ()=> {
    
    const humanChoice = await getHumanChoice();
    const computerChoice = getComputerChoice();

    playRound(humanChoice, computerChoice);

    const score = document.querySelector("#score");
    const newPara = document.createElement("p");
    newPara.classList.add("Score");
    newPara.textContent =`ROUND ${rounds}: You ${humanScore},
    The computer: ${computerScore}`;
    score.appendChild(newPara);

    if (rounds < 5) {
        const nextDiv = document.createElement("div");
        const nextRound = document.createElement("button");
        nextDiv.classList.add("nextDiv");
        nextRound.classList.add("button");
        nextRound.textContent = "Next round";
        newPara.appendChild(nextDiv);
        nextDiv.appendChild(nextRound);
    
        return new Promise((resolve) => {
            nextRound.addEventListener("click", () => {
                resolve();
            }, { once: true });
        });
    }
};

const loopRounds = async ()=> {
    while (rounds < 5) {
        rounds++;
        await playGame()
    } 

    const finalMessage = document.createElement("p");
    if (humanScore > computerScore) {
        finalMessage.textContent = "You are the best!";
    } else if ( humanScore < computerScore) {
        finalMessage.textContent = "You are the worst in the world!";
    } else {
        finalMessage.textContent = "It's a draw!"
    }

    document.querySelector("#score").appendChild(finalMessage);

}

loopRounds();

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật