如何创建一个可以添加或相乘的数字数组来获得数字答案? [英] How do I make an array of numbers that I can add or multiply to get a numerical answer?

查看:64
本文介绍了如何创建一个可以添加或相乘的数字数组来获得数字答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello JS Coders,



首先,我在互联网上搜索了添加两个数组编号的解决方案。没有任何意义。

我很难理解如何创建一个数字数组,我可以进行简单的数学运算。

var cnum = [4,5 ,2,7];



如果我添加cnum [0]和cnum [2]我希望得到6,但我得到42?



我实际上相信我是世界上最差的程序员,并且很谦虚地承认它。



  var  cnum = [ 4  5  2  7 ]; 
d = cnum [ 0 ] + cnum [ 2 ];
alert( 添加两个数组= d); // 42?

似乎JS正在处理cnum ' s as strings。
如何创建可以预先形成数学的数组?

谢谢,
吉姆

解决方案

你的代码很好,除了一个失踪警告中的逗号。

  var  cnum = [ 4  5  2  7 ]; 
d = cnum [ 0 ] + cnum [ 2 ];
alert( 添加两个数组= + d);


Hello JS Coders,

First I searched the Internet for a solution to adding two array numbers. Nothing made sense.
I am having a hard time understanding how to create an array of numbers that I can do simple math operations on.
var cnum = [4, 5, 2, 7];

If I add cnum[0] and cnum[2] I hope to get 6, but I get 42 ?.

I actually believe I am the worlds worst programmer, and am humble enough to admit it.

   var cnum = [4, 5, 2, 7];
   d = cnum[0] + cnum[2];
   alert("add two array numbers = " d);  // 42 ?

It seems like JS is treating the cnum's as strings.
How can I create arrays that I can preform math on?

Thanks,
Jim

解决方案

You code is fine except one missing comma in alert.

var cnum = [4, 5, 2, 7];
d = cnum[0] + cnum[2];
alert("add two array numbers = " +d);


这篇关于如何创建一个可以添加或相乘的数字数组来获得数字答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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