将随机生成的数字添加到数组中并将它们相加

时间:2023-01-04 21:22:44

im currently doing an assignment where we have a certain amount of people play a game and each player have an attempt of scoring. The scores will be randomly generated from 1-3. The only problem i have is to store the randomly generated value into the array and then summing them up. This way, i can produce a leader board that say something like "congrats (player name) your total score is (total score)). Any suggestion on how to do these's would be great or better yet, any other alternatives would be appreciated as well. So far i've been using a incremental counter to generate the total score but it keeps generating the same number over and over again e.g. (2,2,2,2...) (1,1,1,1,....)

我正在做一个任务,我们有一定数量的人玩游戏,每个玩家都有尝试得分。分数将从1-3中随机生成。我唯一的问题是将随机生成的值存储到数组中,然后将它们相加。通过这种方式,我可以制作一个排行榜,上面写着“祝贺(玩家姓名)你的总分是(总分)”。任何关于如何做这些的建议都会很好或者更好,任何其他选择都会被赞赏为好吧。到目前为止,我一直在使用增量计数器来产生总分,但它一遍又一遍地生成相同的数字,例如(2,2,2,2 ......)(1,1,1,1, ....)

<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function main()
    {
    randomnumber()
    totalscore()
    }
function randomnumber()
{
    var randomnumber;
    randomnumber = Math.random()*3;
    return(Math.floor(randomnumber+0.5));

}
function totalscore() 
{
    var n;
    var score = 0;
    number = randomnumber();
    for (n = 0 ; n < 11 ; ++n)
    {
        if (number == 0)
		{
            score = score + 0;
        }
        else if (number == 2)
        {
            score =score + 2;
        }
        else if (number == 3)
        {
            score =score + 3;
        }
    }
		document.write(score)
}


</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
<INPUT NAME = "dobutton" TYPE = "button" value = "Start game" on Click = "game()"> 
<INPUT NAME = "dobutton" TYPE = "button" value = "Leaderboard" on Click = "leader()">
</BODY>
</HTML>

2 个解决方案

#1


This may help, although you should try first before posting for solutions.

这可能有所帮助,尽管您应该在发布解决方案之前先尝试一下。

Create an empty array:

创建一个空数组:

var myArray = [];

Add values to array (from your randomnumber() generator):

向数组添加值(来自randomnumber()生成器):

myArray.push(randomnumber());
myArray.push(randomnumber());
myArray.push(randomnumber());

Function to sum the values of some array (this is perhaps the most primitive but faster/efficient way to do it):

函数来对某些数组的值求和(这可能是最原始但更快/更有效的方法):

var sumElements = function(someArray) {
    if (someArray == null) return false;
    var sum = 0;
    for (var i = 0, len = someArray.length; i < len; i++) {
        sum += someArray[i];
    }
    return sum;
}

Call sumElements to find the sum:

调用sumElements找到总和:

sumElements(myArray);

#2


Here is the simplest way to do what you need

这是做你需要的最简单的方法

var randomArray = [];
var randomSum = 0;
randomArray.push(randomnumber());
randomArray.push(randomnumber());
randomArray.push(randomnumber());

for(var i=0; i<randomArray.lenth; i++){
   randomSum += randomArray[i];
}

#1


This may help, although you should try first before posting for solutions.

这可能有所帮助,尽管您应该在发布解决方案之前先尝试一下。

Create an empty array:

创建一个空数组:

var myArray = [];

Add values to array (from your randomnumber() generator):

向数组添加值(来自randomnumber()生成器):

myArray.push(randomnumber());
myArray.push(randomnumber());
myArray.push(randomnumber());

Function to sum the values of some array (this is perhaps the most primitive but faster/efficient way to do it):

函数来对某些数组的值求和(这可能是最原始但更快/更有效的方法):

var sumElements = function(someArray) {
    if (someArray == null) return false;
    var sum = 0;
    for (var i = 0, len = someArray.length; i < len; i++) {
        sum += someArray[i];
    }
    return sum;
}

Call sumElements to find the sum:

调用sumElements找到总和:

sumElements(myArray);

#2


Here is the simplest way to do what you need

这是做你需要的最简单的方法

var randomArray = [];
var randomSum = 0;
randomArray.push(randomnumber());
randomArray.push(randomnumber());
randomArray.push(randomnumber());

for(var i=0; i<randomArray.lenth; i++){
   randomSum += randomArray[i];
}