JS Pick Random Quotes on Refresh Page

I create my site, and I want every time the user refreshes the page, a new random joke appears instead of the previously selected one. This is the script I have right now:

function ShowJoke(){
  var Joke=["Jokes", "go", "here"];
  var Pick = Math.floor(Math.random() * (Joke.length));
  document.write(Joke[Pick]);
}
+3
source share
3 answers

Your code looks great. But you have syntax errors. In addition, you do not call the final

function ShowJoke()
  {
    var Joke = [
    	'Joke1',
    	'Joke2',
    	'Joke3',
    	'Joke4',
    	'Joke5',
    	'Joke6'
    ];
    var Pick = Math.floor(Math.random() * (Joke.length));
    document.write(Joke[Pick]);
  }
  
  document.addEventListener("load", ShowJoke());
Run code
+1
source

I can only answer with the information you provided, so if something is wrong, tell me so that I can fix it.

So, what am I taking from this question, do you want to load a new joke when loading the page. "every time the page updates a new random quote, not the last"

. - "allow-same-origin" , , , - XAMPP.

- , cookie, , , , cookie, cookie .

// Array of Jokes
var Jokes = [
"I just got fired from my job at the keyboard factory. They told me I wasn't putting in enough shifts.",
"We'll we'll we'll...if it isn't autocorrect.",
"Q. Which type of vegetable tries to be cool, but is only partly successful at it?\n\nA. The radish.",
"The world tongue-twister champion just got arrested. I hear they're gonna give him a really tough sentence."
];

// function to check cookie (true if exists, false if not)
function checkCookie(){
    var joke = getCookie();
    if (joke != "") {
      return true;
    } else {
      return false;
    }
}
// set the cookie so can be referenced later
function setCookie(cvalue){
    var cname = "joke";
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// actually acquire the cookie and read it
function getCookie() {
    var cname = "joke";
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function ShowJoke(){
  let randomNum = ~~(Math.random() * Jokes.length); // pick a random number from 0 to length of jokes
  if(checkCookie()){ // check if cookie exists
    while(Jokes[randomNum] != getCookie()) randomNum = ~~(Math.random() * Jokes.length);
    } // while cookie joke != generated joke
  document.getElementById('Joke').textContent = Jokes[randomNum]; // set content
  setCookie(Jokes[randomNum]); // set cookie
}
window.onload = ShowJoke(); // run on window load
<p id="Joke"></p>
+2

You just need to fix typos

function ShowJoke()
  {
    var Joke=[1, 2, 3, 4, 5, 6, 7];
    var Pick = Math.floor(Math.random() * (Joke.length));
    document.write(Joke[Pick]);
  }

and actually call the function

ShowJoke();

EDIT:

Turns out op doesn't want to repeat the same joke, so we could do something like this:

function ShowJoke()
  {
    var RawJoke=[1, 2, 3, 4, 5, 6, 7];
    var ToldJokes = localStorage.getItem("jokes");
    if (ToldJokes) {
        ToldJokes = JSON.parse(ToldJokes);
    } else {
        ToldJokes = [];
    }
    var Joke = [];
    for (var i = 0; i < RawJoke.length; i++)
        if (ToldJokes.indexOf(RawJoke[i]) === -1) 
            Joke.push(RawJoke[i]);
    if (Joke.length === 0) {
        ToldJokes = [];
        Joke = RawJoke;
    }
    var Pick = Math.floor(Math.random() * (Joke.length));
    ToldJokes.push(Joke[Pick]);
    localStorage.setItem("jokes", JSON.stringify(ToldJokes));
    document.write(Joke[Pick]);
  }
+1
source

Source: https://habr.com/ru/post/1695282/


All Articles