+ 6
Random HTML output
I would like to know how i can write a code that prints a random HTML snippet on the Output
1 Odpowiedź
+ 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.