当命令行顺序重要吗? [英] When command line order matters?

查看:140
本文介绍了当命令行顺序重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用错误的术语,我刚开始编码所以请道歉(请纠正我)。我正在进行以下编码挑战:给定一个具有多个值的数组,编写一个函数,该函数返回一个只包含原始数组的最大值,最小值和平均值的新数组。

I'm new to coding so apologies (and please correct me) if I use wrong terminology. I was doing the following coding challenge: Given an array with multiple values, write a function that returns a new array that only contains the maximum, minimum, and average values of the original array.

这是我第一次尝试时提供的代码:
首次尝试

This is the code I came up with during my first attempt: First attempt

最后,我通过试验和错误确定问题是我的arrnew变量的位置。我还修正了平均表达式,正确的算法是:正确的算法

Eventually I figured out through trial and error that the problem was the location of my arrnew variable. I also fixed the average Expression and the correct algorithm is: Correct algorithm

我假设,因为max,min和avg正在改变(有更好的术语吗?),var arrnew将接收这些新值,但显然不是这样。我想我的问题是什么时候行的秩序很重要?或者更具体地说,是否有任何简单的规则或原则我应该知道,以帮助我更好地了解命令行编码(我不知道这是否称为命令行编码)。

I assumed that since the max, min, and avg were changing (is there a better term for this?), the var arrnew would pick up these new values but obviously that wasn't the case. I guess my question is when does line order matter? Or probably more specifically, are there any simple rules or principles I should be aware of to help me better understand command line coding (I dont know if this is called command line coding).

感谢

推荐答案



When you do this:

var arrnew = [max, min, avg];

这会放置 max min avg 未来对 max min avg 的更改将包含

This places a copy of the contents of max, min, avg into the array. Future changes to what max, min and avg contain will not affect the array in any way.

所以,如果你这样做;

So, if you do;

var max = 3, min = 1, avg = 2;
var arrnew = [max, min, avg];    // 3, 1, 2
max = 5;
console.log(arrnew);             // 3, 1, 2

注意, arrnew 不会受到 max min avg 包含。这只是Javascript(和大多数语言的工作原理)。

Note, the contents of arrnew are not affected at all by subsequent changes to what max, min or avg contain. This is just how Javascript (and most languages work).

这篇关于当命令行顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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