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?

Progate(JS繰り返し処理)学びまとめ

Posted at

for文

ex.js
for(let number=1;number<=100;number++){
  console.log(number);
}

配列

Ex.js
// 定数animalsに、指定された配列を代入してください
const animals = ["dog","cat","sheep"];
// 定数animalsを出力して下さい
console.log(animals);

// 配列の1つ目の要素を出力してください
console.log(animals[0]);
// 配列の3つ目の要素を出力してください
console.log(animals[2]);

// lengthを用いて条件式を書き換えてください
for (let i = 0;i<animals.length;i++) {
  console.log(animals[i]);
}

オブジェクト

  • 配列を作るとき→[]
  • オブジェクトを作るとき→{}
  • {}の中にカンマ区切りでプロパティを入れていく
ex.js
// 定数characterを定義し、指定されたオブジェクトを代入してください
const character = {name:"にんじゃわんこ",age:14};
console.log(character.name);


//オブジェクトを要素にもつ配列
const characters = [
  {name: "にんじゃわんこ", age: 14},
  {name: "ひつじ仙人", age: 1000}
];
// charactersの1つ目の要素をコンソールに出力してください
console.log(characters[0]);
// charactersの2つ目の要素の「name」に対応する値をコンソールに出力してください
console.log(characters[1].name);

const characters = [
  {name: "にんじゃわんこ", age: 14},
  {name: "ひつじ仙人", age: 100},
  {name: "ベイビーわんこ", age: 5},
];
// for文を完成させてください
for (let i=0;i<characters.length ;i++) {
  console.log("--------------------");
  // 定数characterを定義してください
  const character = characters[i];
  // 「名前は〇〇です」を出力してください
  console.log(`名前は${character.name}です`);
  // 「〇〇歳です」を出力してください
  console.log(`${character.age}歳です`);
}



//undefined
const characters = [
  {name: "にんじゃわんこ", age: 14},
  {name: "ひつじ仙人", age: 100},
  {name: "ベイビーわんこ", age: 5},
  {name: "とりずきん"}
];

for (let i = 0; i < characters.length; i++) {
  console.log("--------------------");
  const character = characters[i];
  console.log(`名前は${character.name}です`);
  // if文を追加してください
  if(character.age==undefined){
    console.log("年齢は秘密です");
  }else{
    console.log(`${character.age}歳です`);
  }
}

演習

Ex.js
//演習①
const cafe = {
  name: "Progateカフェ",
  businessHours: {
    // businessHoursの値に指定されたオブジェクトを代入してください
   opening:"10:00(AM)",
   closing:"8:00(PM)"
  }
};

// 「店名:〇〇」を出力してください
console.log(`店名:${cafe.name}`);

// 「営業時間:〇〇から△△」を出力してください
console.log(`営業時間: ${cafe.businessHours.opening}から${cafe.businessHours.closing}`);

//演習②
const cafe = {
  name: "Progateカフェ",
  businessHours: { 
    opening: "10:00(AM)",
    closing: "8:00(PM)"
  },
  // menusプロパティに配列を代入してください
  menus:["コーヒー","紅茶","チョコレートケーキ"]
};

console.log(`店名: ${cafe.name}`);
console.log(`営業時間:${cafe.businessHours.opening}から${cafe.businessHours.closing}`);
console.log(`----------------------------`);
console.log("おすすめメニューはこちら");
// for文を用いて配列menusの中身を表示させてください
for(let i=0;i<cafe.menus.length;i++){
  console.log(cafe.menus[i]);
}

0
0
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
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?