make loop for if

hello
5 Replies
NIMA
NIMA3mo ago
function random_number(){

random_keeper = Math.floor(Math.random() * 4);

if(random_keeper === 0){
question_peeker = question_1;
}
if(random_keeper === 1){
question_peeker = question_2;
}
if(random_keeper === 2){
question_peeker = question_3;
}
if(random_keeper === 3){
question_peeker = question_4;
}








}
function random_number(){

random_keeper = Math.floor(Math.random() * 4);

if(random_keeper === 0){
question_peeker = question_1;
}
if(random_keeper === 1){
question_peeker = question_2;
}
if(random_keeper === 2){
question_peeker = question_3;
}
if(random_keeper === 3){
question_peeker = question_4;
}








}
is there any way to make this loop ? if i want to add any question i need to put antother if ! is there any way to make loop for
let question_1 = [1,'what dose html mean',0,'html','css','grid','flex'];

let question_2 = [2,'what dose php mean',1,'not php','php','mayby php','hph'];

let question_3 = [3,'what dose css mean',0,'css','not css','mayby css','sss'];

let question_4 = [4,'what dose laravel mean',3,'work','boz',' css','laravel'];
let question_1 = [1,'what dose html mean',0,'html','css','grid','flex'];

let question_2 = [2,'what dose php mean',1,'not php','php','mayby php','hph'];

let question_3 = [3,'what dose css mean',0,'css','not css','mayby css','sss'];

let question_4 = [4,'what dose laravel mean',3,'work','boz',' css','laravel'];
im making a exam question app and for each question im making a array
Jochem
Jochem3mo ago
const questions = [question_1, question_2, question_3, question_4];
function random_number(){
random_keeper = Math.floor(Math.random() * 4);

question_peeker = question[random_keeper];
}
const questions = [question_1, question_2, question_3, question_4];
function random_number(){
random_keeper = Math.floor(Math.random() * 4);

question_peeker = question[random_keeper];
}
NIMA
NIMA3mo ago
so u mean i just put all the question in 1 other array ?
Jochem
Jochem3mo ago
yeah though I'd also not use an array for the questions, much better to use an object:
const question_1 = {
id: 1,
question: "What does HTML mean?",
answer_index: 0,
answers: ["html","css","grid","flex"]
}
const question_1 = {
id: 1,
question: "What does HTML mean?",
answer_index: 0,
answers: ["html","css","grid","flex"]
}
it also looks like your function is modifying the global scope directly, which isn't a good idea. You've got something like let question_peeker; somewhere, and you're setting its value in the function? It's generally a good idea to have functions only ever modify their own scope, and return things if you need data out of them:
function random_number(){
random_keeper = Math.floor(Math.random() * 4);

return question[random_keeper];
}

let question_peeker = random_number();
function random_number(){
random_keeper = Math.floor(Math.random() * 4);

return question[random_keeper];
}

let question_peeker = random_number();
also, that function does not return a random number, so you should probably rename it to something more appropriate. Naming is hard, but also super important
NIMA
NIMA3mo ago
tnx for help i am fixing the problem u say
Want results from more Discord servers?
Add your server