+ 3
Please help me in this code👇.
I am making an project and i am facing an problem👇 const b = [{ a: "name1" b: "rarity" }, { a: "name2" b: "rarity" }, { a: "name3" b: "rarity" }, { a: "name3" b: "rarity" }, { a: "name4" b: "rarity2" }, { a: "name2" b: "rarity2" }] My project is based on a game.So it has a long list like this, and in this project I pick a random character from the entire list but I want to add a feature to it. With this feature we will randomly select the character of that rarity after selecting it from the entire list. So what should I do to randomly select any rarity from it and reveal the character of that rarity.
16 Antworten
+ 5
Raja Rawat you would more than likely use something like this to randomize the characters
id = Math.floor(Math.random()*25)
And
switch(id){ case 1: name1; rarity; break; // inside the switch
+ 5
Raja Rawat something like this
id = Math.floor(Math.random()*3)
console.log(id);
switch(id){ case 0:
name = "shelly";
rarity = "starting";
catagory = "";
break;
case 1:
name = "barley";
rarity = "rare";
catagory= "";
break;
case 2:
name = "brock";
rarity = "rare";
catagory = "";
break;
}
console.log(name,rarity,catagory);
+ 1
See here to understand question
https://code.sololearn.com/WwguF7C6mL72/?ref=app
+ 1
You can first create a dictionary object that groups the characters by their rarity. Then, you can randomly select a rarity from the dictionary, and randomly select a character from the list of characters with that rarity.
Here's an example code that does that:
+ 1
const b = [{
a: "name1",
b: "rarity1"
}, {
a: "name2",
b: "rarity1"
}, {
a: "name3",
b: "rarity1"
}, {
a: "name4",
b: "rarity2"
}, {
a: "name5",
b: "rarity2"
}, {
a: "name6",
b: "rarity2"
}];
// group characters by rarity
const charactersByRarity = {};
b.forEach((character) => {
const rarity = character.b;
if (!charactersByRarity[rarity]) {
charactersByRarity[rarity] = [];
}
charactersByRarity[rarity].push(character);
});
// randomly select a rarity and a character from that rarity
const rarityKeys = Object.keys(charactersByRarity);
const randomRarity = rarityKeys[Math.floor(Math.random() * rarityKeys.length)];
const charactersWithRarity = charactersByRarity[randomRarity];
const randomCharacter = charactersWithRarity[Math.floor(Math.random() * charactersWithRarity.length)];
console.log("Random character:", randomCharacter.a);
console.log("Random rarity:", randomRarity);
+ 1
Azeez Khan can you give me example
+ 1
BroFar bro i want to select an option like
option 1 for rare
option 2 for super rare
So if we select option 1 then only one rare brawler will select from the list
+ 1
Sure
+ 1
Here you go
+ 1
const b = [
{ a: "name1", b: "rare" },
{ a: "name2", b: "rare" },
{ a: "name3", b: "rare" },
{ a: "name4", b: "super rare" },
{ a: "name5", b: "super rare" },
{ a: "name6", b: "super rare" }
];
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question("Select an option (1 for rare, 2 for super rare): ", option => {
const rarity = option === '1' ? 'rare' : 'super rare';
// filter characters by selected rarity
const charactersWithRarity = b.filter(character => character.b === rarity);
// randomly select a character from the filtered list
const randomCharacter = charactersWithRarity[Math.floor(Math.random() * charactersWithRarity.length)];
console.log("Random character:", randomCharacter.a);
console.log("Selected rarity:", rarity);
readline.close();
});
+ 1
Select an option (1 for rare, 2 for super rare): 1
Random character: name1
Selected rarity: rare
+ 1
Azeez Khan
Error👇
require is not define?
+ 1
Azeez Khan but if we have to select multiple input
0
Hii
0
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css2?family=Berkshire+Swash&family=Quicksand:wght@300;400&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="style.css">
<link href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css" rel="stylesheet">
<body>
<a class="profile_link" href="https://www.sololearn.com/Profile/20617819/?ref=app">👻lost👻 </br>Copyright©-Tนktนk</a>
<!--
❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃
Created by Tนktนk
❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃❁❃
-->
<main>
<div class="container">
<div class="body"></div>
<div class="head"></div>
<div class="face"></div>
<div class="nack"></div>
<div class="cover"></div>
<div class="left__hand"></div>
<div class="right__hand"></div>
<div cla