Python
JavaScript
FizzBuzz
Swift

【言語学習メモ】FizzBuzz

More than 1 year has passed since last update.


JavaScript


fizzbuzz.js

const seq = 100

let result = []

for ( var i = 0; i <= seq; i ++ ) {
if ( i == 0 ) {
out = i.toString()
} else if ( i % 3 == 0 & i % 5 == 0 ) {
out = 'FizzBuzz'
} else if ( i % 3 == 0 ) {
out = 'Fizz'
} else if ( i % 5 == 0 ) {
out = 'Buzz'
} else {
out = i + ''
}
// 配列に要素を追加
result[i] = out
}
// 結果を表示
console.log(result)



Swift


fizzbuzz.swift

let seq: Int = 100

var result: [String] = []
var out: String

for i in 0 ... seq {
if i == 0 {
out = String(i)
} else if i % 3 == 0 && i % 5 == 0 {
out = "FizzBuzz"
} else if i % 3 == 0 {
out = "Fizz"
} else if i % 5 == 0 {
out = "Buzz"
} else {
out = String(i)
}

result.append(out)
}

print(result)



Python


fizzbuzz.py

seq = 100

result = []

for i in range(seq):
if i == 0:
out = str(i)
elif i % 3 == 0 and i % 5 == 0:
out = 'FizzBuzz'
elif i % 3 == 0:
out = 'Fizz'
elif i % 5 == 0:
out = 'Buzz'
else:
out = str(i)

result.append(out)

print(result)