I have an app that is essentially a random number gen and assigns it to some horse breeds. I need to have a condition that checks if ANY variable AND only 1 variable is equal to 1.
I want something like this that assigns the value that is passed to a variable and then checks it to a user-inputted variable
I tried to mockup something like this, but it seems to only pass boolean results
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");
}
}
I am setting the variables like this:
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;
so I need to repeat the process if none of them work.
CodePudding user response:
I understand you need to get a random name based on some of them wich condition equals 1, you can accomplish this with a simple recursion.
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());
Hope it helps.
CodePudding user response:
The following code configures a result array object. Arrays are objects with nonnegative integer-like keys.
So we enumerate the competitors and push result positions calculated at random into the arrays corresponding to those positions.
Confirming the winner is as simple as reading position 1 in the array (and checking there is only one entry).
const getPosition = (entrantCount = 0) => Math.floor(Math.random() * entrantCount) 1
const race = (entrants) =>
entrants.reduce((acc, entrant) => (acc[getPosition(entrants.length)].push(entrant), acc), [...Array(entrants.length 1)].map(() => []))
const raceUntilAWinner = async (entrants) => {
const result = race(entrants)
if(result[1]?.length === 1) return result
return raceUntilAWinner(entrants)
}
;(async () => {
const entrants = ['Appaloosa', 'French Trotter', 'Fresian', 'Hackney', 'Faflinger']
const result = await raceUntilAWinner(entrants)
console.log(`The winner is ${result[1]}.`)
})()