study-note

ループ文

目次

forループ

in

fruits = ['apple', 'banana', 'cherry']

for fruit in fruits
  console.log fruit

of

obj = {name: 'Alice', age: 25}

for key, val of obj
  console.log "#{key}: #{val}"

whileループ

i = 0

while i < 3
  console.log i
  i++

レンジ演算子

for文との組み合わせ

for i in [0..4]
  console.log i

ガード(when)句

ループ中に条件をつけるときは、whenを使う

for i in [1..10] when i % 2 == 0
  console.log i

サンプルコード

console.log "偶数"
for i in [1..10] when i % 2 == 0
  console.log i
console.log "辞書"
obj = {a:1, b:2, c:3}
for key, val of obj
  console.log "#{key}: #{val}"
console.log "カウント"
j = 0;
while j <= 4
  console.log j
  j++

配列・オブジェクト操作