Random HTML output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Random HTML output

I would like to know how i can write a code that prints a random HTML snippet on the Output

30th Jun 2020, 5:11 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
1 Answer
+ 4
You could save each snippet to a variable, then add each snippet variable to an array. Then get a random number from 0 to length of array - 1. Use random number as an index for the array to output the snippet. let h1 = "<h1>header 1</h1>"; let h2 = "<h2>header 2</h2>"; let h3 = "<h3>header 3</h3>"; ..... let snippets = [h1, h2, h3, ....]; let index = Math.floor(Math.random() * snippets.length - 1) + 1; snippet = snippets[index]; then output it to the console or to the document. console.log(snippet); document.write(snippet); etc.
30th Jun 2020, 6:04 PM
ChaoticDawg
ChaoticDawg - avatar