跳至內容

JavaScript/迴圈/練習

來自華夏公益教科書,開放的書籍,面向開放的世界

主題:迴圈

for(第一部分)

[編輯 | 編輯原始碼]

1 結果是什麼?

"use strict";

for (const i = 3; i <= 6; i++) {
  alert(i);
} //

從 3 到 5 的數字
從 3 到 6 的數字
執行時錯誤
以上都不是

2 結果是什麼?

"use strict";

for (let i = 10; i >= 0; i--) {
  alert(i);
  i--;
} //

從 10 到 0 的數字
從 10 到 0 的偶數
從 10 到 0 的奇數
執行時錯誤
以上都不是

3 結果是什麼?

"use strict";

for (let i = 5; i < 10; ) {
  alert(i);
  i = i + 3;
  alert(i);
} //

5, 8
5, 8, 8
5, 8, 11
5, 8, 8, 11
執行時錯誤
以上都不是

4 結果是什麼?

"use strict";

const myString = "abc";
for (let i = myString.length - 1; i >= 0; i--) {
  alert(myString[i]);
} //

a, b, c
c, b, a
執行時錯誤
以上都不是


for(第二部分)

[編輯 | 編輯原始碼]

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);

for..in, for..of, entries()

[編輯 | 編輯原始碼]

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 結果是什麼?

"use strict";

const myArray = [10, 11, 12];

for (const x in myArray) {
  alert(x);
} //

for (const x of myArray) {
  alert(x);
} //

0, 1, 2 後面跟著 10, 11, 12
10, 11, 12 後面跟著 0, 1, 2
執行時錯誤

2 結果是什麼?

"use strict";

const myArray = [10, 11, 12];

for (const x in myArray) {
  alert(myArray[x]);
} //

0, 1, 2
10, 11, 12
執行時錯誤


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));
華夏公益教科書