学習記録 javascript 合計金額比較プログラム
var items = [
{ name: 'じゃがいも', price: 50, count: 3 },
{ name: 'にんじん', price: 60, count: 1 },
{ name: '豚肉', price: 200, count: 1 },
{ name: '玉ねぎ', price: 50, count: 2 }
];
// targetPriceより小計の高額な商品を出力したい
function higherPrice (targetPrice) {
for (var i = 0; i < items.length; i++) {
// for文の繰り返しの回数を、itemsオブジェクトの中身の数にする。するとオブジェクトの中身が全て出力される。
if (items[i].price * items[i].count > targetPrice){
// 呼び出したものを出力する。items[i]はitemsのi番目の中身という意味
// items[i].priceはオブジェクトのi番目のpriceプロパティの値という意味
console.log(items[i].name);
// 上の式の結果をnameの値で表示する
}
}
}
higherPrice(120);