我正在學習 JS,我剛剛完成了一個專案,代碼如下:
const menu = {
_meal: "",
_price: 0,
//Let set the new value of meal only if it's a string.
set meal(mealToCheck) {
if (typeof mealToCheck === "string") {
return this._meal = mealToCheck;
}
},
//Let set the new value of price only if it's a number.
set price(priceToCheck) {
if (typeof priceToCheck === "number") {
return this._price = priceToCheck;
}
},
//If both setters are true, then return a message using them, otherwise return message saying the values are wrong.
get todaysSpecial() {
if (this._meal && this._price) {
return `Today's Special is ${this._meal}, for just ${this._price}£!!`;
} else {
return "Meal and Price wasn't entered correctly!!";
}
}
};
//Arrays for the meal options and respective prices, also a constant to get a random number in the array range.
const meals = ["Pizza", "Steak", "Pie", "Roast", "Moussaka", "Lasagne", "Tacos"];
const prices = [/*Pizza*/9, /*Steak*/13, /*Pie*/11, /*Roast*/14, /*Moussaka*/9, /*Lasagne*/10, /*Tacos*/9];
const random = Math.floor(Math.random() * meals.length);
//Assigns a new random value from the arrays. I used a single randomizer so that you can combine a plate to its price by the index number.
menu.meal = meals[random];
menu.price = prices[random];
//Check if the number of items in the meals and prices array it's equal, and if it is, creates the menu of the day string.
if (meals.length === prices.length) {
console.log(menu.todaysSpecial);
} else {
console.log("The number of prices and meals don't match!!");
}
在代碼的最后,我添加了幾個陣列和一個Math.random,這樣每次我運行它時,它都會給我一個不同的值。
現在我試圖找到一種方法讓隨機值給出唯一值,直到它達到陣列長度,然后重新啟動。目前我在模擬星期幾的陣列中有 7 個專案,我希望每個專案每周出現一次,然后重置。
我知道如何通過遵循陣列索引順序來做到這一點,但是我無法找到一種隨機執行的方法,任何輸入?
uj5u.com熱心網友回復:
您可以結合您的價格和餐點陣列使其更簡單,然后隨機播放。
const menu = {
_meal: "",
_price: 0,
//Let set the new value of meal only if it's a string.
set meal(mealToCheck) {
if (typeof mealToCheck === "string") {
return (this._meal = mealToCheck);
}
},
//Let set the new value of price only if it's a number.
set price(priceToCheck) {
if (typeof priceToCheck === "number") {
return (this._price = priceToCheck);
}
},
//If both setters are true, then return a message using them, otherwise return message saying the values are wrong.
get todaysSpecial() {
if (this._meal && this._price) {
return `Today's Special is ${this._meal}, for just ${this._price}£!!`;
} else {
return "Meal and Price wasn't entered correctly!!";
}
}
};
//Arrays for the meal options and respective prices, also a constant to get a random number in the array range.
let meals = [
{ name: "Pizza", price: 9 },
{ name: "Steak", price: 13 },
{ name: "Pie", price: 11 },
{ name: "Roast", price: 14 },
{ name: "Moussaka", price: 9 },
{ name: "Lasagne", price: 10 },
{ name: "Tacos", price: 9 }
];
// Used to shuffle your meals array
function shuffle(array) {
let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex]
];
}
return array;
}
meals = shuffle(meals);
// Iterate meals
for (let i = 0; i < meals.length; i ) {
menu.meal = meals[i].name;
menu.price = meals[i].price;
console.log(menu.todaysSpecial);
}
uj5u.com熱心網友回復:
每次從陣列中選擇值時都很容易將其洗掉,并且當陣列結束時將陣列重置為初始值對于沒有亂數的問題,它應該是這樣的
Rand=Math.random()*array.lenght;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/485831.html
標籤:javascript 数组 随机的
