如何将数组/值传递给Javascript函数 [英] How to pass Array/Values to a Javascript function

查看:119
本文介绍了如何将数组/值传递给Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用在线资源的项目的Javascript,但我不知道如何使这个功能正常工作。

I am learning Javascript for a project using online resources however i don't know how to get this function working.

var results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results.length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

在函数之后,我使用:
checkWinner(results);

Just after the function, i use: checkWinner(results);

在HTML文件中,我使用alert来显示变量获胜者。但它显然不起作用。我意识到这是我对范围和全局变量的理解的问题。

In a HTML file i use alert to display the variable winner. But it obviously doesn't work. I realise it is a problem with my understanding of scope and global variables.

推荐答案

var Results =[[a1,a2,a3,a4,a5]];
var winner = 0;

function checkWinner (results)
{ 
  for (var i = 0; i < results[0].length; i++)
    if (results[0][i] > 50)
    {
      winner = results[0][i];

    }
}

checkWinner(Results);

为避免名称冲突,请从大写字母中命名全局变量。
同样在你的代码中,你调用父数组的长度。您需要指定Child数组的长度

To avoid name collisions name global variables from capital case. Also in your code you call the length of the "parent" array. You need to specify the length of the "Child" array

这篇关于如何将数组/值传递给Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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