我有一個應用程式,它本質上是一個亂數生成器,并將其分配給一些馬品種。我需要有一個條件來檢查是否有任何變數并且只有 1 個變數等于 1。
我想要這樣的東西,它分配傳遞給變數的值,然后將其檢查給用戶輸入的變數
我試圖模擬這樣的東西,但它似乎只傳遞布爾結果
var balance = ''
var target = getText("horseIn")
var leverage = getNumber("levIn")
var horses = {
Appaloosa: getNumber("h1pos"),
frenchTrotter: getNumber("h2pos"),
Fresian: getNumber("h3pos"),
Hackney: getNumber("h4pos"),
Faflinger: getNumber("h5pos"),
pasoFino: getNumber("h6pos"),
}
var winner = null;
onEvent("startM", "click", function( ) {
setScreen("selectAmount");
});
onEvent("submitBet", "click", function( ) {
if (getNumber("userAmount") >0){
setScreen("horseScreen");
updateScreen();
console.log(horses)
while (winner == null) {
for (i in horses) {
if (horses[i] == 1){
winner = i;
}
}
}
} else {
setText("userAmount","Please input an amount!");
}
console.log(winner)
});
function updateScreen(){
setText("h1pos", Math.floor(Math.random() * 6) 1);
setText("h2pos", Math.floor(Math.random() * 6) 1);
setText("h3pos", Math.floor(Math.random() * 6) 1);
setText("h4pos", Math.floor(Math.random() * 6) 1);
setText("h5pos", Math.floor(Math.random() * 6) 1);
setText("h6pos", Math.floor(Math.random() * 6) 1);
}
function pnl(){
setScreen("endScreen");
if (target == winner) {
console.log("Winner");
}
}
我正在設定這樣的變數:
let Appaloosa = Math.floor(Math.random() * 6) 1;
let frenchTrotter = Math.floor(Math.random() * 6) 1;
let Fresian = Math.floor(Math.random() * 6) 1;
let Hackney = Math.floor(Math.random() * 6) 1;
let Faflinger = Math.floor(Math.random() * 6) 1;
let pasoFino = Math.floor(Math.random() * 6) 1;
所以如果它們都不起作用,我需要重復這個程序。
uj5u.com熱心網友回復:
我知道您需要根據其中一些條件等于 1 來獲得一個隨機名稱,您可以通過簡單的遞回來完成此操作。
function getWinnerHorse() {
const horses = {
Appaloosa: 1 === Math.floor(Math.random() * 6) 1,
"French Trotter": 1 === Math.floor(Math.random() * 6) 1,
Fresian: 1 === Math.floor(Math.random() * 6) 1,
Hackney: 1 === Math.floor(Math.random() * 6) 1,
Faflinger: 1 === Math.floor(Math.random() * 6) 1,
"Paso Fino": 1 === Math.floor(Math.random() * 6) 1,
};
const horse = Object.entries(horses).find((item) => item[1]);
if (!horse) return getWinnerHorse();
return horse[0];
}
console.log(getWinnerHorse());
希望能幫助到你。
uj5u.com熱心網友回復:
以下代碼配置了一個結果陣列物件。陣列是具有非負整數類鍵的物件。
所以我們列舉競爭對手并將隨機計算的結果位置推送到與這些位置對應的陣列中。
確認獲勝者就像讀取陣列中的位置 1 一樣簡單(并檢查只有一個條目)。
const getPosition = (entrantCount = 0) => Math.floor(Math.random() * entrantCount) 1
const resultArray = (entrantCount) => [...Array(entrantCount 1)].map(() => [])
const race = (entrants) =>
entrants.reduce((acc, entrant) =>
(acc[getPosition(entrants.length)].push(entrant), acc),
resultArray(entrants.length))
const raceUntilOneWinner = (entrants) => {
let result = null
while(result?.[1]?.length !== 1)
result = race(entrants)
return result
}
const entrants = ['Appaloosa', 'French Trotter', 'Fresian', 'Hackney', 'Faflinger']
const result = raceUntilOneWinner(entrants)
console.log(`The winner is ${result[1]}.`)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/524227.html
上一篇:如何在接受此類輸入的函式內計算2Dchar陣列的行數?
下一篇:對于給定數量的馬蹄p和數量的馬k
