JavaScript/循环/练习
外观
< JavaScript | 循环
主题:循环
1. 编写一个使用两个嵌套循环的脚本,并显示以下字符串
1,
1, 2,
1, 2, 3,
1, 2, 3, 4,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
点击查看解决方案
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const START = 1;
const END = 10;
let myString = "";
for (let o = START; o <= END; o++) {
myString = "";
for (let i = 1; i <= o; i++) {
myString += i + ", ";
}
alert(myString);
}
2. 编写一个使用两个嵌套循环的脚本,并显示以下字符串,包括撇号
Next line is: "1, 3, 5, 7, 9,"
Next line is: "1, 3, 5, 7,"
Next line is: "1, 3, 5,"
Next line is: "1, 3,"
Next line is: "1,"
(额外挑战:删除最后一个逗号)
点击查看解决方案
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const START = 1;
const END = 10;
let myString = "";
for (let o = END; o >= START; o = o - 2) {
myString = 'Next line is: "';
for (let i = 1; i <= o; i = i + 2) {
myString += i + ", ";
}
myString += '"';
alert(myString);
}
3. 编写一个脚本,创建一个边长为 4 的“正方形”。它由一个对角线上的“x”和其它位置上的点组成。提示:创建一个空字符串,并在循环中将字符追加到字符串中。换行符是“\n”。最后,显示字符串。
- 使用
for
循环。 - 在第二步中,只更改一个语句以创建一个相应的 6x6 正方形。
x...
.x..
..x.
...x
点击查看解决方案
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const LENGTH = 4;
let result = "";
for (let i = 0; i < LENGTH; i++) {
for (let j = 0; j < LENGTH; j++) {
if (i === j) {
result = result + "x";
} else {
result = result + ".";
}
}
result = result + "\n";
}
alert(result);
// to create a 6x6 quadrat, just change the variable LENGTH to '6'
4. 编写一个脚本,创建一个带有两个箭头头的“箭头”:“<======>”。箭头的长度由一个变量决定。更改此变量将导致不同大小的箭头。
<=====>
or
<===================>
点击查看解决方案
"use strict";
// by convention, fundamental constants like PI or such constants that
// determine the behavior of your program are written in uppercase
const LENGTH = 4;
// left head
let result = "<";
for (let i = 0; i < LENGTH; i++) {
result = result + "=";
}
// right head
result = result + ">";
alert(result);
1. 编写一个脚本
- 创建一个包含你的一些个人特性的对象
- 在 for..in 循环中显示所有属性(键和值)
- 使用 Object.entries() 方法显示所有属性(键和值)。
点击查看解决方案
"use strict";
// an example
const myObj = {firstName: "Marilyn", familyName: "Monroe", born: 1953};
for (const key in myObj) { // 'in' delivers the keys
alert('First: ' + key + ' / ' + myObj[key]);
}
// 'entries()' delivers key and value within an array-element
for (const [key, val] of Object.entries(myObj)) {
alert('Second: ' + key + ' / ' + val);
}
2. 编写一个脚本
- 创建一个包含所有小于 10 的奇数的数组
- 使用 for..of 循环创建一个包含所有这些数字以及分隔符的字符串
- 在循环后显示字符串
点击查看解决方案
"use strict";
const myArray = [];
for (let x = 1; x < 10; x = x + 2) {
myArray.push(x);
}
let result = "";
for (const value of myArray) {
result += value + '; ';
}
alert(result);
1. 编写一个脚本
- 定义一个数组
const arrIntegers = [3, 8, -3, 0];
- 使用
.forEach
构造来填充第二个数组,其中包含第一个数组的二次方值(9, 64, ...) - 显示第二个数组
点击查看解决方案
"use strict";
// declare both arrays
const arrIntegers = [3, 8, -3, 0];
const arrQuadrat = []; // empty
// compute the quadrats
arrIntegers.forEach((elem) => arrQuadrat.push(elem * elem));
// show the result
arrQuadrat.forEach(elem => alert(elem));