为什么我拉了错误?当然的Array.push工作 [英] Why am I pulling an error? array.push course work

查看:141
本文介绍了为什么我拉了错误?当然的Array.push工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图书已与FF code为摄氏转换为华氏页,我们被要求重写/简化它。

\r
\r

函数convertToCentigrade(degFahren)\r
{\r
    VAR degCent;\r
    degCent = 5/9 *(degFahren - 32);\r
    返回degCent;\r
}\r
\r
VAR degFahren =新阵列(212,32,-459.15);\r
VAR degCent =新的Array();\r
VAR loopCounter;\r
\r
为(loopCounter = 0; loopCounter&下; = 2; loopCounter ++)\r
{\r
    degCent [loopCounter] = convertToCentigrade(degFahren [loopCounter]);\r
}\r
\r
为(loopCounter = 2; loopCounter> = 0; loopCounter--)\r
{\r
    文件撰写(值+ loopCounter +是+\r
        degFahren [loopCounter] +华氏度);\r
    文件撰写(这是+ degCent [loopCounter] +\r
        摄氏度< BR />中);\r
}

\r

\r
\r

我的版本:

\r
\r

VAR degFar = [212,32,-459.15]\r
变种degCent = [];\r
            \r
功能转换(输入){\r
    结果=(5/9 *(输入 - 32));\r
    返回结果;\r
}\r
            \r
对于(i = 0; J = degFar.length; I< = j的;我++){\r
    degCent.push(转换(degFar [I]));\r
    的document.write(degCent [I]);\r
}

\r

\r
\r

我拉着一个错误(明显),但我不明白我在做什么错。


解决方案

您for循环导致错误。您尝试使用两个变量,'J'和'我'。我不知道你正在尝试使用J(上,你从来没有宣布试图把它初始化为degFar.length前J顶部)来完成的。

用你的逻辑一致,我想摆脱'J' - 你不需要它。除去J以及使用后的i它应该是这个样子:

 为(i = 0; I< degFar.length;我++){
 degCent.push(转换(degFar [I]));
 的document.write(degCent [I] +< BR>中);
}

希望这有助于。

Book has page with the ff code to convert Centigrade to Fahrenheit, we've been asked to rewrite/simplify it.

function convertToCentigrade(degFahren)
{
    var degCent;
    degCent = 5/9 * (degFahren - 32);
    return degCent;
}

var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter;

for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{
    degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);
}

for (loopCounter = 2; loopCounter >= 0; loopCounter-- )
{
    document.write("Value " + loopCounter + " was " + 
        degFahren[loopCounter] + " degrees Fahrenheit");
    document.write(" which is " + degCent[loopCounter] + 
        " degrees centigrade<br />");
}

My version:

var degFar = [212, 32, -459.15];
var degCent = [];
            
function convert(input) {
    result = (5/9 * (input - 32));            
    return result;
}
            
for (i = 0; j = degFar.length; i <= j; i++) {
    degCent.push(convert(degFar[i]));
    document.write(degCent[i]);
}

I pulling an error (obviously), but I don't understand what I'm doing wrong.

解决方案

Your for loop is causing the error. You try to use two variables, 'j' and 'i'. I'm not sure what you are trying to accomplish by using 'j' (on top of that you never declared 'j' before trying to initialize it to degFar.length).

Keeping with your logic, I would get rid of 'j'-- you don't need it. After removing 'j' and using 'i' it should look something like this:

for (i = 0; i < degFar.length; i++) {
 degCent.push(convert(degFar[i]));
 document.write(degCent[i] + "<br>");
}

Hope this helps.

这篇关于为什么我拉了错误?当然的Array.push工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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