迭代输出一个数组的每个元素是 JavaScript 中的常见需求, for 循环可以做到这一点。

下面的代码将输出数组 arr 的每个元素到控制台:

var arr = [10,9,8,7,6];
for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}

记住数组的索引从零开始的,这意味着数组的最后一个元素的下标是:数组的长度 - 1。我们这个循环的 条件 是 i < arr.length,当 i 的值为 长度-1 的时候循环就停止了。

任务

声明并初始化一个变量 total0。使用 for 循环,使得 total 的值为 myArr 的数组中的每个元素的值的总和。

#221 Iterate Through an Array with a For Loop的更多相关文章

  1. [Javascript] Iterate Over Items with JavaScript's for-of Loop

    In this lesson we will understand the For Of loop in Javascript which was introduced in ES6. The for ...

  2. [Javascript] The Array filter method

    One very common operation in programming is to iterate through an Array's contents, apply a test fun ...

  3. [Javascript] The Array map method

    One very common operation in programming is to iterate through an Array's contents, apply a function ...

  4. postgresql:array & foreach

    --数组: SELECT (ARRAY['{101, 111, 121}', '{201, 211, 221}'])[1]::text[]; SELECT (ARRAY['{101, 111, 121 ...

  5. Twitter OA prepare: Equilibrium index of an array

    Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to ...

  6. vector & array

    private static const NUM_LOOPS:int = 15; public function VectorTest():void { var vector:Vector.<i ...

  7. ros msg array

    #include <stdio.h> #include <stdlib.h> #include "ros/ros.h" #include "std ...

  8. [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  9. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

随机推荐

  1. 爬虫学习--MOOC爬取豆瓣top250

    scrapy框架 scrapy是一套基于Twisted的异步处理框架,是纯python实现的爬虫框架,用户只需要定制开发几个模块就可以轻松实现一个爬虫,用来抓取网页内容或者各种图片. scrapy E ...

  2. 微信小程序获取二维码参数

    var scene = decodeURIComponent(options.scene)

  3. Python开发【初始篇】:Linux下安装Python3

    Linux系统默认自带python2.6的版本,这个版本被系统很多程序所依赖,所以建议不要轻易删除,除非你能解决其他程序的依赖问题.如果使用最新的Python3需要进行编译安装源码包,这样就对系统默认 ...

  4. [leetcode]61. Rotate List旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  5. python_字符编码

    一 了解字符编码的知识储备 1.计算机基础知识 2.电脑存放组成: 硬盘 - 内存 -(二级缓存.一级缓存.cpu寄存器)- cpu # cpu交互的是用户能识别的数据:字符# 硬盘中最终存储的数据: ...

  6. Day3 -4.9!受到毕设的突然袭击,一脸蒙蔽,学习暂时停止,明晚继续

    PS:啊啊啊啊,慌张的不行,不详的预感终于爆发了,第二次毕设评图好突然,没办法了,竹径和学习突然搁置,明晚健身/建模/补更Day3,感到崩溃 ————————————————————————————— ...

  7. DynamoDB

    https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html Creating a  New  Table I ...

  8. docker log driver

    驱动程序 描述 none 容器没有日志可用,docker logs 什么都不返回 json-file 日志格式化为 JSON.这是 Docker 默认的日志驱动程序. syslog 将日志消息写入 s ...

  9. java用POI操作excel——随便写一下,最基础的东西

    前两天部门实施在做一个东西,需要把客户放在Excel中的数据导入到Oracle数据库中,我就想着直接写一个模板,必要的时候改一下实体类应该可以解放实施同事的双手,不过在实际写的过程中,还是碰到很多问题 ...

  10. HDU2035

    #include <bits/stdc++.h> using namespace std; int fastpow(int a,int b,int k) { ; while(b) { ) ...