Putting it together · 統合
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Putting it together
- Real programs combine variables, loops, functions, arrays and objects.
- A
for...ofloop reads each item of an array directly — no index needed.
日本語
統合
- 実際のプログラムでは、変数、ループ、関数、配列、オブジェクトを組み合わせて使用します。
for...ofループは配列の各項目を直接読み込み、インデックスは必要ありません。
function total(nums) {
let t = 0;
for (const n of nums) {
t = t + n;
}
return t;
}
console.log(total([4, 8, 6])); // → 18
Explore · 探索
for...of builds the total · for...ofで合計を計算
for...of reads each item directly. Watch t add them up, one at a time. · for...ofは各要素を直接読み取ります。tが一つずつ加算していく様子を見てください。
English
Searching and counting
- Loop through the array, and keep a counter (or a flag) as you go.
日本語
探索とカウント
- 配列をループで処理しながら、進行状況に応じてカウンタ(またはフラグ)を維持してください。
function countAbove(nums, limit) {
let count = 0;
for (const n of nums) {
if (n > limit) {
count++;
}
}
return count;
}
console.log(countAbove([3, 7, 2, 8], 4)); // → 2
English
Common mistakes
- Build in small steps and use
console.logto check as you go. - Keep the HTML, CSS and JavaScript doing their own jobs.
日本語
よくあるミス
- 小さなステップで組み立て、進めながら
console.logを使って確認してください。 - HTML、CSS、JavaScriptそれぞれが独立した役割を果たし続けるようにしてください。
English
Now you try
- Write each function using a loop.
- Press Check answer to test it.
日本語
あなたも試してみよう
- ループを使用して各関数を記述してください。
- Check answer を押して確認する。
Write a function total(nums) that returns the sum of every number in the array nums. · 関数total(nums)を作成し、配列numsのすべての数値の合計を返してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write a function biggest(nums) that returns the largest value in the array. · 関数biggest(nums)を作成し、配列における最大値を返してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write a function countAbove(nums, limit) that returns how many numbers are greater than limit. · 関数countAbove(nums, limit)を作成し、>limitである数値の個数を返してください。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。