简体   繁体   English

如何创建用户提供的数字数组并将其映射到数字立方体的数组?

[英]How can I create an array of user-provided numbers and map that to an array of the numbers' cubes?

My application is having a user supply a set of numbers. 我的应用程序要求用户提供一组数字。 The user determines how many numbers they will provide, and then I prompt them accordingly. 用户确定他们将提供多少个号码,然后我相应地提示他们。 I want to create an array of the provided input numbers using a function. 我想使用一个函数创建一个提供的输入数字的数组。 Then I want to use another function to calculate the cube of each of these numbers, and put those in a new array. 然后,我想使用另一个函数来计算这些数字中的每一个的多维数据集,并将其放入新数组中。 I am finding it difficult to create the array of cubes. 我发现很难创建多维数据集数组。 Can anyone help me with this? 谁能帮我这个?

 var BR = "<br />"; var ES = " "; /********* FUNCTIONS *********/ // Enter any functions in this region function fncube(c) { var result = 1; var tro; for (var j = 0; j < c.length; j++) { result = c[j] * c[j] * c[j]; tro = new Array(result); } return tro; } function displayarray(d) { var mm = new Array(); for (var m = 1; m < d.length; m++) { mm = mm.push[m]; } return mm; } /********* MAIN *********/ function main() { var userinput = new Array(); //var m = new Array(); var usersize; var tilavg; usersize = parseInt(window.prompt("Enter number of cubes you would like to make: ")); for (var i = 0; i < usersize; i++) { userinput[i] = parseInt(window.prompt("Enter your number #: " + (i + 1))); document.writeln(BR + "Number you entered " + " for making cube is : " + userinput[i] + ES) document.writeln(BR + "The cube is " + fncube(userinput) + ES); var cc = fncube(userinput); xx = new Array(cc); //var z = displayarray(cc); } document.writeln(BR + "The array of input values is: " + cc); document.writeln(BR + "The array of cubes is: " + xx); } main(); 

The JavaScript Array map method natively supports transforming an array to another array of the same size where the translation function is the same for each element. JavaScript Array map方法本机支持将一个数组转换为另一个相同大小的数组,其中每个元素的翻译功能都相同。 If you define your transformation function as the cubing operation (in JS that's Math.pow(x, 3) ), then you can easily translate from one array to another. 如果将转换函数定义为cubing操作(在JS中为Math.pow(x, 3) ),则可以轻松地从一个数组转换为另一个数组。

Consider the example below. 考虑下面的示例。 I use jQuery's version of map to transform input elements to an array of their values, then use the JS map method to transform that array to an array containing the cubes of all the values. 我使用jQuery的map版本将input元素转换为其值的数组,然后使用JS map方法将该数组转换为包含所有值的多维数据集的数组。 Give it a try. 试试看。 You should be able to tweak this to fit your use-case once you understand how map works. 了解map工作原理后,您应该可以对此进行调整以适合您的用例。

 $(function() { $("input").change(function() { var nums = $("input").map(function() { return this.value || undefined; }).get(); $("#nums").text(nums); $("#cubes").text(nums.map(function(i) { return Math.pow(i, 3); })); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h4>Enter four numbers</h4> <input /><br /> <input /><br /> <input /><br /> <input /> <div id="nums"></div> <div id="cubes"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM