使用JavaScript数组和循环 [英] Using JavaScript for arrays and loops

查看:137
本文介绍了使用JavaScript数组和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是真正的新JavaScript和我被困在我得到了锻炼。我使用TextPad因为这是我一直要求用本练习。

I am really new to JavaScript and i am stuck on an exercise i have been given. I am using TextPad as this is what i have been asked to use for this exercise.

基本上我需要有一个输入参数是一个数/整数,然后有12个输出参数,显示输入已被除以12,并且每个输出参数具有不超过previous或值下一个超过1。

Basically i need to have one input parameter that is a number/integer and then have 12 output parameters that show the input has been divided by 12 and that each output parameter has a value that does not exceed the previous or next one by more than 1.

我已经写了下面code到目前为止使用数组和一个循环,但我只是不知道怎样还是怎样在获得输出参数显示阵列平均分配的每个值方面的事情。

I have written the below code so far using an array and a for loop but i just dont know how or what to do in terms of getting the output parameters to show each value of the array divided evenly.

var amount = "30";

var camels = ["cam1", "cam2", "cam3", "cam4", "cam5", "cam6", "cam7", "cam8", "cam9", "cam10", "cam11", "cam12"]

for(i=0;i<camels.length;i++){
    WScript.echo(camels[i] + "|" + amount/12);
}

for(i=1; i<13; i++){
    n = Math.random();
    o = n * 12;
    p = o + 1;
    q = Math.floor(p);
}

这是真的困惑我所以任何帮助将是AP preciated但请记住袒露我是一个完整的新手。

This is really confusing me so any help would be appreciated but please bare in mind i am a complete novice.

推荐答案

这是一门功课的问题,您得到一个完整的解决方案一无所获(或许比一个A等,你会不值得!)

This is a homework question, and you gain nothing by getting a complete solution (other than perhaps an A which you wouldn't deserve!)

相反,我可以给你一些三分球,希望这将让你正确的答案。

Instead I can give you some pointers, which will hopefully get you to the right answer.

首先要注意的是,这样做数学计算的时候要要处理数字,因此适当地改变你的第一行

The first thing to be aware of is that when doing mathematical calculations you want to be dealing with numbers, so change your first line appropriately

var amount = 30;

(请注意,如果你是让用户输入这个号码,你会希望确保它的数字,并将其转换为数字 - 用户输入几乎总是一个字符串使用 parseInt函数(&lt;使用者。输入方式&gt; 10)

做的第二件事是由12楼使用 Math.floor 来得到一个整数这个数除以你的金额。在这个例子中,你将获得 24

The second thing to do is divide your amount by 12. Floor this number using Math.floor to get an integer. In this example you will get 24.

(认为上述的另一种方法是考察模运算符,它可以用来代替)

(Another way to think of the above is to investigate the modulo operator %, which can be used instead)

在这一点上你可以创建一个数组,12个元素(每个骆驼)每个元素应该有2(重新presenting 2货各项目)的计数。 (见REF1)

At this point you can create an array, with 12 elements (one for each camel) each element should have a count of 2 (representing 2 cargo items each). (see ref1)

通过这个例子,你会最终有一个 6 的剩余部分,​​仅剩下要做的就是分配其余的事情 6 随机你的骆驼之一。

With this example you will end up with a remainder of 6 and the only thing left to do is distribute the remaining 6 randomly among your camels.

您可以通过将所有12头骆驼到一个数组,循环 6 (或者,无论你的其余的是)次做到这一点,并挑选<$ C $之间的随机数C> 0 和 {骆驼数组长度} -1 。分配该货物到选定的骆驼和骆驼阵列删除骆驼。这将确保没有人骆驼随机被分配一个以上的额外的船货。

You can achieve this by putting all 12 camels into an array, loop 6 (or, whatever your remainder was) times and pick a random number between 0 and {length of camel array}-1. Assign that cargo to the selected camel and remove that camel from the camel array. This ensures that no one camel randomly gets assigned more than one extra cargo.

与通常的程序设计中,最后一位应该被分解成独立管理的步骤。

As with programming in general, that last bit should be broken down into individual manageable steps.


  1. 创建具有12个元素(其中,12是骆驼的数量)的阵列。这个数组的元素应该是数字0-11重新presenting数组的索引早期

  2. 创建一个循环重复以下code一次为每个其余

  3. 在循环中:

    • 挑0之间的随机数(见REF2),并在1中创建的阵列的长度。

    • 使用该随机数递增先前创建的数组中的计数

    • 从1中创建的数组中删除的元素。

不要误会;这里有2个数组。一个创建早得多 - 它有12个元素,并跟踪每个骆驼货物的数量,另一种是暂时的,用于其骆驼已被分配一个额外的货物跟踪。前者从来不改变长度,而后者被尺寸减小骆驼被分配一个额外的货物。

Don't get confused; there are 2 arrays here. One you created much earlier - it has 12 elements and tracks the number of cargos on each camel, the other is temporary, used for tracking which camel has been assigned an "extra" cargo. The former never changes length, whereas the latter gets reduced in size as camels are assigned an extra cargo.

参考资料:

  • ref1: arrays
  • ref2: Math.random
  • ref3: removing a specific element from an array

这篇关于使用JavaScript数组和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆