简体   繁体   English

将数组值赋给变量

[英]assigning an array value to a variable

I came across a problem which I did manage to solve but I honestly do not understand how this works. 我遇到了一个问题,我确实设法解决了,但老实说我不明白这是怎么回事。

var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
//My code
for (var i = 0; i < array.length; i++) {
    if (array[i] > largest) {
        largest = array[i];
    }
}

console.log(largest);

How does my array assign a value to my variable within the if statement? 我的数组如何在if语句中为我的变量赋值?

Why does this version not work? 为什么这个版本不起作用?

array[i] = largest;

I'm very new to programming and I believe that understanding fundamental things as small as this can help many beginners become competent programmers. 我对编程非常陌生,我相信理解像这样小的基本事物可以帮助许多初学者成为称职的程序员。

if(array[i] > largest) {
           largest = array[i];
        }

array[i] is 3 and largest is 0 then if condition passes and 如果条件通过,则array[i]3largest0

largest = array[i];

assign value right to left now the largest value is 3 . 现在分配到左值对largest值是3

so next cycle array[i] is 6 so next 6 > 3 true , it will again change the largest to 6 . 所以下一个循环array[i]6所以接着6 > 3真,它将再次将largest to 6

like it will give you largest number. 喜欢它会给你最大的数字。

I think you are mistaking the assignment statement in programming with the equality statement in mathematics. 我认为你在使用数学中的等式语句编程时错误地使用赋值语句。

Assignment vs Equality 作业与平等

In your code, when you say largest = array[i] you are assigning to the variable 'largest' the value of the element on the ith position of the array. 在你的代码中,当你说max = array [i]时,你要为变量'maximum'分配数组第i个位置的元素值。 Notice here that the array is not changing any of its values in this assignment. 请注意,数组不会更改此赋值中的任何值。

On the other hand, array[i] = largest would try to assign to the ith position on the array the present value of the variable 'largest'. 另一方面,array [i] = maximum会尝试将数组中的第i个位置分配给变量'maximum'的当前值。

This code is trying to calculate the largest value in an array of values.. 此代码尝试计算值数组中的最大值。

Doing

array[i] = largest;

would modify the input itself which is undesired... you need to find the largest value from the given input.. and not modify the given input location to some largest value. 会修改输入本身,这是不希望的...您需要从给定输入中找到最大值..而不是将给定输入位置修改为某个最大值。

That is why 这就是为什么

largest = array[i] 

is correct. 是正确的。

When you assign a value to a variable, the variable has to be on the left of the assignment operator (the equals sign "="). 为变量赋值时,变量必须位于赋值运算符的左侧(等号“=”)。

This way has the variable on the left: 这种方式左边有变量:

largest = array[i];

It puts the array[i] value into the largest variable, and so it works. 它将array [i]值放入最大的变量中,因此它可以工作。

This way is backwards: 这种方式是倒退的:

array[i] = largest;

It puts the largest value into the array[i] element, and your array ends up with a bunch of zeros in it. 它将最大的值放入array [i]元素中,并且您的数组最终会有一堆零。

I know this is an old thread, but i've this issue today and want to help others developers with proper source of knowledge, comming directly from Kyle Simpson. 我知道这是一个老线程,但我今天遇到这个问题,并希望帮助其他开发人员获得正确的知识来源,直接来自Kyle Simpson。

You don't know JS 你不懂JS

Operator assignment has his own way to work. 操作员分配有自己的工作方式。 It calculate the statement of the right "=" to later asign it, to the left. 它计算右边的语句“=”以便稍后将其标记为左侧。 This is very important to know how does it work. 知道它是如何工作的非常重要。

Example: 例:

var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
//My code
for (var i = 0; i < array.length; i++) {
    if (array[i] > largest) {
        largest = array[i];
    }
}

console.log(largest);

Here, largest = array[i]; 这里, largest = array[i];

JS, is calculate the statement of the right and assing to the left JS,是计算 右边的声明并向左边做出贡献

So have fun with the book and play. 所以玩书和玩耍吧。

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

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