如何将骰子卷排列成阵列

如何将骰子卷排列成阵列

本文介绍了如何将骰子卷排列成阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的stackoverflowers

Dear stackoverflowers,

有人可以在这项运动中进一步帮助我吗?

Could someone help me further with this excercise:

掷骰子40次。抛出必须放在数组中。如果罚球与前一个罚球相同,则必须将其分组在方括号之间。每次投掷将花费您1分,如果连续投掷两个相同的数字,您将获得5分。打印出该用户的信息(例如: Congratz!您获得了5分),以及该用户还剩下多少分。我真的不知道用户从多少点开始,而只是给它40分。

Throw a dice 40 times. The throws has to be put in an array. If a throw is the same as the previous one, it has to be grouped between brackets. It'll cost you 1 point per throw, and if you throw two the same numbers in a row, you get 5 points. Print the info out for the user(like: "Congratz! You earned 5 points") , and how many points the user has left. I dont really know from how many points the user starts but lets just give it 40.

到目前为止,这是我的代码

This is my code so far

<html>
<head>
    <script>
function rollDice() {
           var die1 = document.getElementById("die1");
           var status = document.getElementById("status");

           var d1 = Math.floor(Math.random()*6) +1;
           console.log("You rolled "+d1+".");
           if(d1)

  }

  </script>
</head>
<body>
    <div id="die1" class="dice">0</div>

    <button onclick="rollDice()"> Roll the dice </button>
    <h2 id="status" style="clear:left":> </h2>
</body>

我想知道如何将其放入一个数组中,如果抛出的异常与上一个相同,则将其记录到控制台5分。
我是一个初学者,所以请多包涵。

Id like to know how to put this into an array and if a throw is the same as the previous one, it logs to the console 5 points.I'm a beginner so please bear with me.

在此先感谢

Youssef

推荐答案

您可以声明一个数组,如下所示:

YOu can declare an array as follows:

var diceRolls = [];

要向数组中添加内容:

diceRolls.push(diceRoll);

检查以前的结果是否相等。注意shoudl还要检查数组中是否存在上一个元素。

To check previous result for equality. Note shoudl also check the previous element exists in the array.

if (diceRolls[diceRolls.length - 1] === diceRoll)
     // do stuff

希望可以帮助您入门。 W3cschools是入门的好方法

Hope that gets you started. W3cschools is a great way to get started

这篇关于如何将骰子卷排列成阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:40