学習日
2026年7月1日
名前
堀内道
自分で作った図形(コードと画像)
✅ 練習問題 4-2-2
function setup() {
let items = ["やくそう", "たいまつ", "せいすい"];
print("所持アイテム: " + items);
print("");
let useditem = items.pop();
print("勇者は「" + useditem + "」を使った!");
print("所持アイテム: " + items);
usedItem = items.pop();
print("勇者は「" + usedItem + "」を使った!");
print("所持アイテム: " + items);
usedItem = items.pop();
print("勇者は「" + usedItem + "」を使った!");
print("アイテムを全て使い切りました");
}
📷 実行結果

✅ 練習問題 4-2-3
function setup() {
let items = ["やくそう", "たいまつ", "せいすい", "キメラのつばさ", "かぎ"];
print("所持アイテム: " + items);
print("");
let index = items.indexOf("たいまつ");
print("「たいまつ」の位置: " + index);
index = items.indexOf("まほうのかぎ");
print("「まほうのかぎ」の位置: " + index);
} 📷 実行結果

✅ 練習問題 4-2-3
function setup() {
let items = ["やくそう", "たいまつ", "せいすい", "キメラのつばさ", "かぎ"];
print("所持アイテム: " + items);
print("");
// ここにコードを書いてください
if (items.indexOf("たいまつ") !== -1) {
print("「キメラのつばさ」を持っています!");
} else {
print("「キメラのつばさ」を持っていません。");
}
if (items.indexOf("まほうのかぎ") !== -1) {
print("「まほうのかぎ」を持っています!");
} else {
print("「まほうのかぎ」を持っていません。");
}
}
📷 実行結果

✅ 練習問題 4-2-4
function setup() {
let items = ["やくそう", "たいまつ", "せいすい", "キメラのつばさ", "かぎ"];
print("最初: " + items);
print("");
items.splice(2, 1, "まんげつそう");
print("「せいすい」を「まんげつそう」に交換した!");
items.splice(0, 0, "やくそう(大)");
print("「やくそう(大)」を手に入れた!");
items.splice(5, 1);
print("「かぎ」を捨てた!");
print("所有アイテム:" + items);
}📷 実行結果

✅ 練習問題 4-2-5
function setup() {
let prices = [150, 80, 300, 50, 200];
print("価格一覧: " + prices);
items.sort();
print(prices);
}数字はsort()で並び替えることはできない
✅ 練習問題 4-2-6
function setup() {
let prices = [480, 120, 320, 80, 650];
print("元の順番:" + prices);
prices.sort(function(a, b){
return a - b;
});
print("安い順:" + prices);
prices.sort(function(a, b){
return b - a;
});
print("高い順:" + prices);
}📷 実行結果

✅ 練習問題 4-2-7
function setup() {
let rank1 = ["ピンスティック","ひのきのぼう"];
let rank2 = ["きょうじゃのこん","せいどうのツメ","くさかりがま","シーブスナイフ"];
let rank3 = ["バトルアックス","はがねのツメ","はがねのムチ","はがねのやり"];
let rank4 = ["オルテガのオノ","パパスの剣","シルバークロー","あくまのムチ"];
let rank5 = ["メタルキングの剣","ラーミアのムチ","ゾンビキラー","はやぶさの剣"];
let tickets = 30; // 手持ちチケット
let n = 0; // 累計回数
while (tickets > 0) {
tickets--;
n++;
let r = random();
let item = "";
if (r < 0.03) {
print(n +"回目" + rank5[Math.round(floor(random(3)))] + "(★★★★★)");
} else if (r < 0.03 + 0.07) {
print(n + "回目" + rank4[Math.round(floor(random(3)))] + "(★★★★)");
} else if (r < 0.03 + 0.07 + 0.15) {
print(n + "回目" + rank3[Math.round(floor(random(3)))] + "(★★★)");
} else if (r < 0.03 + 0.07 + 0.15 + 0.30) {
print(n + "回目" + rank2[Math.round(floor(random(3)))] + "(★★)");
} else {
print(n +"回目" + rank1[Math.round(floor(random(1)))] + "(★)");
}
}
print("合計" + n + "回引きました");
}
📷 実行結果

✅ 練習問題 4-2-8
function setup() {
let rank1 = ["ピンスティック","ひのきのぼう"];
let rank2 = ["きょうじゃのこん","せいどうのツメ","くさかりがま","シーブスナイフ"];
let rank3 = ["バトルアックス","はがねのツメ","はがねのムチ","はがねのやり"];
let rank4 = ["オルテガのオノ","パパスの剣","シルバークロー","あくまのムチ"];
let rank5 = ["メタルキングの剣","ラーミアのムチ","ゾンビキラー","はやぶさの剣"];
let kekka = []
let tickets = 10; // 手持ちチケット
let n = 0; // 累計回数
while (tickets > 0) {
tickets--;
n++;
let r = random();
let item = "";
if (r < 0.03) {
kekka.push("5:" + rank5[Math.round(floor(random(3)))] );
} else if (r < 0.03 + 0.07) {
kekka.push("4:" + rank4[Math.round(floor(random(3)))] );
} else if (r < 0.03 + 0.07 + 0.15) {
kekka.push("3:" + rank3[Math.round(floor(random(3)))] );
} else if (r < 0.03 + 0.07 + 0.15 + 0.30) {
kekka.push("2:" + rank2[Math.round(floor(random(3)))] );
} else {
kekka.push("1:" + rank1[Math.round(floor(random(1)))] );
}
kekka.sort();
print(kekka);
}
}
📷 実行結果

✅ 練習問題 4-2-9
| 特定の要素を探す | indexof indexof(“何か “); |
| 捨てる、交換する | splice splice(2 , 1); 場所と個数 |
| 追加 | push push(” 何か” ); |
| 取り出し、消去 | pop pop(); |
| 整理する | sort,reverse sort( ); reverse( ); |
まとめ・感想
4-2-7がずっとできなかった、理由はrank5[ ]の[ ]が( )になっていたのとrandom()の中の数字が3だったら1から3までだと思っていたけれど本当は1.1111111~から2.9999999999だということが分かった。なのでだたのrandomだと少数でダメなのでfloorで切り捨てをしなければならなかった。