HomeХобби и стильRelated VideosMore From: Ralph Phillips

Use JavaScript to Display Random Trivia on Click

70 ratings | 5460 views
Create an array of random trivia facts and use a JavaScript function to display a different fact each time a user clicks a button.
Html code for embedding videos on your blog
Text Comments (15)
pattyorigami (10 days ago)
How would you do this with audio files randomly played on click of that button? None of the tutorials or forums I've visited have helped me make this work yet!
GABA8 play. (3 months ago)
Not Working😭 html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link rel="icon" href="title.gif"> <link type="text/javascript" href="javascript.js"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <button class="rubiccube" onclick="rubiccube()">Klikněte Pro Randomizaci</button> <h4 id="randomizeCuBE"></h4> </body> </html> js( *javascript* ): var RubicCube = [] RubicCube[0] = "R B U' L2 R U2 L2 D F R' F L R F2 R B2 D' F' L' B"; RubicCube[1] = "D2 R' D2 U' F' R' D' U2 R2 L F U' F D U L2 U2 D2 F' L2"; RubicCube[2] = "B2 D U2 F B U2 R2 L2 D2 L D2 U R B' D' B' D2 R L U' "; RubicCube[3] = "F' D F' B' D2 F2 R D L2 R' U' B F' U' D2 L' B L' B' F' "; RubicCube[4] = "F' D2 U' B L' R2 U' F' B R' B2 D2 U2 F D2 B' L R B L' "; RubicCube[5] = "L' D L2 F' L2 D R' L U L2 F D F U2 R D' F' L2 F L "; RubicCube[6] = "B R F2 R D' F R' B' U2 D L' F2 R B U2 F' R2 B2 R D2 "; RubicCube[7] = "L2 U' R' L F U' L2 D F' L D2 B' L2 D F L2 D' U' B' R "; RubicCube[8] = "F' R2 F' L2 D U2 F' D B' F' R2 L2 U2 B2 F' U2 F L' B' D2 "; RubicCube[9] = "D L2 D L2 U' D R2 U2 B2 F' R' U' B2 U' D2 R U R' F L' "; RubicCube[10] = "U2 F R2 L F2 U2 D' B2 R L2 F U2 L2 U2 L' F2 U D L' U' "; RubicCube[11] = "B' D' U' F2 R D' F' R F' U' R2 B2 R F D' L2 R' D' U' B "; RubicCube[12] = "D' U R B2 R' U' D' F R U' R' B' U2 R2 L' F B2 L' F2 D "; RubicCube[13] = "U2 F U' B' F2 U2 L2 R' U2 R2 D2 F2 D' L U' B' F2 U B F' "; RubicCube[14] = "B2 D' R' F' D' L2 R2 U R' L' U2 L' R D2 R' B2 R2 B D2 R2 "; RubicCube[15] = "R U2 B2 D2 R2 D' R U R' D2 L R2 F' B' D' R D2 B' R' B "; RubicCube[16] = "L' R2 B' D2 F D F2 R F2 B L U' L R F' U2 D' B' R2 L' "; RubicCube[17] = "R2 D' U2 B' U D B D L R B2 R F2 R L2 D2 U2 F' U B2 "; RubicCube[18] = "B' L' R' U B2 F' U D' L2 R' D2 L' D' U' L' F2 B' U' F L "; RubicCube[19] = "L' B2 R2 B2 U R2 U L2 B2 L2 D2 R2 B R' U' B U D' R L2 "; RubicCube[20] = "R L F2 R U2 B R L B F' L2 U2 R2 U2 D' F D' B' L' F "; function rubiccube(){ var randomizeCUBE = Math.floor(Math.random()*(RubicCube.length)); document.getElementById('randomizeCuBE').innerHTML = RubicCube[randomizeCUBE]; }
XoXo Bes Of The Wall (8 months ago)
i need html for this where i put the var starting ?
Flyspark (8 months ago)
error: cannot read property "length" of undefined any help???
jenny internetcafee (1 year ago)
Thanks for this awesome explanation. your are the best!
Girma Ayele (1 year ago)
hey, you are awesome. great explanation.
liquidsolomon (1 year ago)
I used your tutorial as the logic for craps program. My question is: Am I on the right track for adding the sum of two random numbers pulled from two different arrays? //button event will generate 2 random numbers and add them together var num1 = [1,2,3,4,5,6]; var num2 = [1,2,3,4,5,6]; //var sum1 = [2,3,4,5,6,7,8,9,10,11,12] var sum2 = num1 + num2 function rollDice(){ var randomNumber = Math.floor(Math.random()*(num1 + num2)); document.getElementById("roll").innerHTML = randomNumber[rollDice]; }
liquidsolomon (1 year ago)
Answer to this question: https://stackoverflow.com/questions/45622537/javascript-craps-game-using-two-arrays-to-generate-the-point?answertab=oldest#tab-top
Gmoney Mozart (1 year ago)
Anyone know how after you show a random array to push or slice it into another array so it doesn't pop up twice? I can't seem to figure it out.
Mahtab Daneshnia (2 years ago)
Thanks a million it is very good and easy. But I have a question, I want to have multiple choice questions in my array and I need to use j query instead of "getElementById", what should I do? If you help me with this I will thankful million times. It`s kinda urgent dear Ralph.
Mrdorsett333 (3 years ago)
Thank you so much! Ive been trying to make a program like this forever and all the other sources i looked up made their code way more complex.
Steven Botelho (5 years ago)
Cannot make it work, google says "Uncaught SyntaxError: Unexpected token } index2.html:16" witch is something to do when i call function (onclick) or the JS itself... i have literally copied the text on the video... EDIT: deleted half bottom of HTML page and redid it in notepad, for some reason there where some ?"invisible characters"? messing the code....
Eman C (5 years ago)
I have some web page layouts. How much will you charge to help me launch my website?
SynxS (5 years ago)
Breast fed babies score slightly higher on mental development tests than ones on formula.
egal (5 years ago)
Now how do you program it to check to see if the next random number is the same as the last.. and to tell it to reroll until you have a new random number.. so as to have a new trivia fact every time you push the button?

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.