简体   繁体   English

我已经编写了此代码,但未显示任何输出。 为什么会这样呢?

[英]I have written this code but it is not showing any outputs. Why is this happening?

I have written this code but I am completely not sure the way I did is correct. 我已经编写了这段代码,但是我完全不确定我做的方法是否正确。 Is there anyone that can give me some guidance? 有没有人可以给我一些指导?

  • Create a variable named numbers and assign an empty array to it. 创建一个名为numbers的变量,并为其分配一个空数组。
  • Using a for-loop and the Array.push() method, insert the numbers 0 to 9 into the array you named numbers. 使用for循环和Array.push()方法,将数字09插入命名为数字的数组中。
  • Test that you used the Array.push() method correctly by console logging the first item in the array. 通过控制台记录阵列中的第一项,测试是否正确使用了Array.push()方法。
  • Console log the last item in the array. 控制台记录阵列中的最后一项。
  • Create another variable named car and assign an empty object to it. 创建另一个名为car变量,并为其分配一个空对象。
  • Give the object a property called colour and assign it the value of "black" . 给对象一个名为colour的属性,并将其值指定为"black"

 var numbers = []; for (var i=0; i < 10; i++){ numbers.push(i); console.log(i[1]) console.log(i[9]) } var car = {}; 

var numbers = [];

for(var i=0; i<10; i++) {
    numbers.push(i)
}

console.log(numbers[0]);

var lastIndex = numbers.length - 1;

console.log(numbers[lastIndex]);

var car = {};
car.colour = 'black';

You have pushed numbers to an array numbers , but after this you have mixed up variables numbers and i . 您已经将数字推到数组numbers ,但是在此之后,您将变量numbersi混合了。 Variable i is not an array. 变量i不是数组。 Except this your console.log is in the for loop and this is mistake too. 除此之外,您的console.log处于for循环中,这也是错误的。

I have corrected your code: 我已更正您的代码:

 var numbers = []; for (var i = 0; i < 10; i++){ numbers.push(i); } console.log(numbers[1]); //1 console.log(numbers[9]); //9 var car = {}; car.colour = 'black'; console.log(car.colour); //black 

暂无
暂无

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

相关问题 我已经编写了从框中复制链接的代码,但它是否向我显示以下错误 - I have written code to copy the link from box but is it showing me the following error tensorflow js 模型(从 Keras 转换而来)有 3 个输出。 我怎样才能提取它们? - tensorflow js model (converted from Keras) has 3 outputs. How can I extract them? 为什么我没有显示任何 JSON 和 403 消息? - Why don't I have any JSON showing and a 403 message? R中的DT,格式化单元格输出。 - DT in R, format cell outputs. 我已经为简单的AngularJS JavaScript编写了代码,但无法正常工作,只有根范围正在更新,而其他两个却没有,为什么 - I have written code for simple AngularJS JavaScript but not working properly only root scope is updating but not the other two why 我在我的 JS 代码中编写了 fadeOut 命令,在我的待办事项列表项目中,但是淡出不能正常工作并在谷歌控制台中显示错误? - I have written fadeOut command in my JS code, in my to do list project but the fadeout is not working properly and showing error in google console? 为什么在 VS 代码中数组输出有一个 () 旁边有一个数字? - Why is it that array outputs have a () with a number next to it in VS code? 如何运行我用JavaScript编写的代码? - How do I run code I have written in JavaScript? 我写的代码没有显示输入百分比的结果 - The code i have written doesnt show results on input percentage 为什么会这样? 我在控制台 output 和 UI 中的正确数量的行中得到了正确的显示,但我没有得到任何输出 - Why is this happening? I'm getting correct display in console output and right amount of rows in UI but I'm getting no outputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM