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

Posted at

<配列の取り出し>
let data = ['JavaScript', 'Python', 'PHP', 'Rudy', 'Perl'];
console.log(data[0]);
console.log(data[1]);
console.log(data[2]);
console.log(data[3]);
console.log(data[4]);

<配列の削除>
let data = ['Python', 'PHP', 'JavaScript'];
console.log(delete data[0]);
console.log(data);
console.log(data.length);

let data2 = ['Python', 'PHP', 'JavaScript'];
data2.shift(); //配列の先頭から要素を削除
console.log(data2);
console.log(data2.length);

<カウントダウン>
for (let i = 10; i >= 0; i--) {
console.log(i);
}
/*
for (初期化式; 条件式; 増減式) {
処理A
処理B
...
}
*/

<配列専用の繰り返し構文(連想配列では使えない)>
let data = [ 'apple', 'orange', 'banana' ];
for (const value of data) {
console.log(value);
}
for (let i = 0; i < data.length; i++) {
console.log(data[i]);
}

<時刻・時期ごとにメッセージを表示する方法>

const gozen = new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
const ohiru = new Set([12]);
const gogo = new Set([13, 14, 15, 16, 17, 18]);
const yoru = new Set([19, 20, 21, 22, 23]);

for (let h = -1; h <= 24; h++) {
console.log(\n== ${h}時 ==)
console.log('---< 解法1 >---');
if (h >= 0 && h <= 11) {
console.log('おはようございます');
} else if (h === 12) {
console.log('お昼です。');
} else if (h >= 13 && h <= 18) {
console.log('こんにちは。');
} else if (h >= 19 && h <= 23) {
console.log('こんばんは。');
} else {
console.log('時刻の範囲を超えています。');
}

console.log("---< 解法2 >---");
if (h < 0 || h > 23) {
    console.log('時刻の範囲を超えています。');
} else if (h <= 11) {
    console.log('おはようございます。');
} else if (h === 12) {
    console.log('お昼です。');
} else if (h <= 18) {
    console.log('こんにちは。');
} else {
    console.log('こんばんは。');
}

console.log('---< 解法3 >---');
if (h < 0 || h > 23) {
    console.log('時刻の範囲を超えています。');
} else if (h >= 19) {
    console.log('こんばんは。');
} else if (h >= 13) {
    console.log('こんにちは。');
} else if (h === 12) {
    console.log('お昼です。');
} else {
    console.log('おはようございます。');
}

console.log('---< 解法4 >---');
if (gozen.has(h)) {
    console.log('おはようございます。');
} else if (ohiru.has(h)) {
    console.log('お昼です。');
} else if (gogo.has(h)) {
    console.log('こんにちは。');
} else if (yoru.has(h)) {
    console.log('こんばんは。');
} else {
    console.log('時刻の範囲を超えています。');
}

}

const spring = new Set([3, 4, 5]);
const summer = new Set([6, 7, 8]);
const autumn = new Set([9, 10, 11]);
const winter = new Set([12, 1, 2]);

for (let m = 0; m <= 13; m++) {
if (spring.has(m)) {
console.log(${m}月は春です);
} else if (summer.has(m)) {
console.log(${m}月は夏です);
} else if (autumn.has(m)) {
console.log(${m}月は秋です);
} else if (winter.has(m)) {
console.log(${m}月は冬です);
} else {
console.log('月の指定に誤りがあります。');
}
}

<条件分岐>
let rank = 'B';

switch(rank) {
case 'A' :
console.log('Aランクです。');
break;
case 'B' :
console.log('Bランクです。');
break;
case 'C' :
console.log('Cランクです。');
break;
default :
console.log('ランク外です。');
break;
}

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?