0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScript ループ(繰り返し)

Last updated at Posted at 2023-11-19

for文

//関数定義
function hasOdd(numbers) {

  result = false;
  for(i = 0;i < numbers.length;i++){
    s = numbers[i]
    o = s % 2;
    switch (o) {
      case 0:
        continue;
      default:
        result = true;
        break;
    }
    if(result == true){
      break;
    }
  }
  console.log(result);
}

//関数呼び出し
hasOdd([1, 2, 3, 4, 5])

例えば、このコードは、
受け取った関数の要素の中に、1つでも奇数があった場合には、trueをコンソール出力するプログラムです。

do...while文

while文

for...in文

//関数定義
function square(numbers) {

  for(var item in numbers){
    numbers[item] = numbers[item] * numbers[item]
  }

  console.log(numbers);
}

//関数呼び出し
square([1, 2, 3, 4, 5])

for...of文

//関数定義
function hasOdd(numbers) {

  result = [];
  for(let num of numbers){
    check = num % 2;
    switch (check) {
      case 0:
        break;
      default:
        result.push(num);
        break;
    }
  }
  console.log(result);
}

//関数呼び出し
hasOdd([1, 2, 3, 4, 5])

labelについて

continueについて

breakについて

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?