0
1

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 3 years have passed since last update.

JavaScript 学習用 関数

Posted at

基礎


function sayHello(){
    return "Hello";
}
sayHello();


function sayHello(name) {
    // ここに戻り値を書いてください
    return `Hello ${ name }`;
  }

引数がない場合


function sayHello(name) {
// ここに戻り値を書いてください
  if (!name) {
    return "What is your name?";
  } else {
    return "Hello " + name;
  }
}

文字列を数値に


function add(a, b) {
  // ここに戻り値を書いてください
  return parseInt(a, 10) + parseInt(b, 10);
}

配列処理


function sum(array) { 
  let sum = 0;
  for(let i = 0; i < array.length; i++){
    sum += array[i];
  }
  return sum;
}

fizzBuzz


function fizzBuzz(number){
  if(number % 5 === 0 && number % 3 === 0){
    return "fizzBuzz";
  } else if(number % 3 === 0){
    return "Fizz";
  } else if(number % 5 === 0){
    return "Buzz";
  } else{
    return number;
  }
}
0
1
0

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?