0

I am trying to make it where if one function runs and defines the variable the other function will be able to reconize the function. I'm a little new to Javascript so my code may be just a bunch of jibberish.

function next() {
    var random = Math.floor(Math.random() * 15)
    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who() {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}
0

2 Answers 2

2

Like this:

var random;

function next() {
    random = Math.floor(Math.random() * 15);
    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who() {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. I think this is better than mine because everytime next() is used, random will change.
I tried that but for some reason it says "I am undefined." I had defined the variable things defined earlier. For all my code go link
0

Define the random variable first, and then send it to the functions, like this;

var random = Math.floor(Math.random() * 15);

next(random);
who(random);

function next(random) {

    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who(random) {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}

1 Comment

But then random won't change I need it to change every time I run next()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.