Thank you for the walk-thru. As a newb to JS, how do I run the script and can I package it in an executable as a stand-alone app for a friend to run it from their system? Also, I changed it to fit our needs, but I cannot see it working. Help?
var myElectives = ['Fishing', 'Playground', 'Bike Riding', 'Underwater Basket Weaving', 'Playing with the Teacher', 'Silent Reading', 'Going to the Library']
var lastItem = myElectives.Length - 1
var elective = myElectives[Math.floor(Math.random() * myElectives.length)];
Now, are you planning on displaying the result to the screen? Or would a developer tool console work? The Console Logging tutorial can help with the second part: https://www.kirupa.com/html5/console.htm
For sharing with your friend, all you need to send is an HTML page. There is no need for an executable. Depending on what you answer for how you wish to display the result to the screen, I can help you further
Thank you for responding! I wrapped it in a script tag, but I have a feeling I’m missing a display of the var value.
What I’m going for is for the principal to press a button on the display to randomly select a class for a student. The elementary students are getting to select an elective class for fun. They get a list of options and and will choose their 1st, 2nd, and 3rd preferences. I need the script to take all their selections, group them, and randomly comprise a class list.
Using your suggestions, here is my
[code]
Stalker Elementary School - Elective Class Random Selector
Elective Class Random Selector
[/code]
How do you want to display it? One quick and dirty way is by using an alert statement that will display something in a dialog:
var myElectives = ['Fishing', 'Playground', 'Bike Riding', 'Underwater Basket Weaving', 'Playing with the Teacher', 'Silent Reading', 'Going to the Library'];
var lastItem = myElectives.Length - 1;
var elective = myElectives[Math.floor(Math.random() * myElectives.length)];
alert(elective);
Hey Kirupa,
Great work!!
I need to pick the random number from the array without repetition. As a newbie to JS I tried it but seems like I am missing something. Can you pls help me with it?
I’m sure @senocular or @krilnon will have a much better solution, but what I do is just shuffle the array first and then pick all the items in sequence. This ensures no items is picked up more than once.
Why do we have to use Square Brackets when using Math .floor and Math.random. For eg: ```
let value = myArray[Math.floor(Math.random() * myArray.length)];
The square brackets are used to get an item from an array using an index number:
let foo = myArray[2]; // get the third item
Now what needs to be inside the bracket is a number. You can have any expression inside there that returns a number. The Math.floor and Math.random functions in the example are helping with getting back a number.
Hey, Kirupa! How are you doing?
Sorry to bother you, I have a question to something i been struggling with!
i want to create a “group” basically ^^
e.g.
Not at all. I was just curious because I saw sleep, which is a much requested (but missing) feature in browser JS
Here is one way for you to randomly call one of the functions:
function groupOne() {
channel.send('1')
await sleep(10000)
channel.send('2');
await sleep(10000)
}
function groupTwo() {
channel.send('3')
await sleep(10000)
channel.send('4');
await sleep(10000)
}
let groups = ["one", "two"];
let group = groups[Math.floor(Math.random() * groups.length)];
if (group == "one") {
groupOne();
} else {
groupTwo();
}
You can also have your functions be stored in the array itself, but for just two functions, this indirect approach works fine. Let me know if you’d like to see what that might look like
Oh wow! you’re amazing man!
Thank you so much for this.
I can add more function groups to it freely right?
edit:
while testing it did not work…
this is my current code:
const Discord = require("discord.js");
const client = new Discord.Client();
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
client.on('ready', async () => {
var server = client.guilds.get("716227489860681738");
var channel = server.channels.get("716227489860681743")
console.log(`Logged in as ${client.user.username}, ready to get this bread.`);
for (var i = 0; i < 1000000; i++) {
function groupOne() {
channel.send('1')
await sleep(10000)
channel.send('2');
await sleep(10000)
}
function groupTwo() {
channel.send('3')
await sleep(10000)
channel.send('4');
await sleep(10000)
}
let groups = ["one", "two"];
let group = groups[Math.floor(Math.random() * groups.length)];
if (group == "one") {
groupOne();
} else {
groupTwo();
}
}
});
client.login(process.env.token)
Do you see an error i am not?
its giving me 0 errors…
What are you trying to do with the for loop? Yes, you can add more function groups freely. I will share a revised version of the code to simplify that by removing the if statement