0

I am trying to make a website where it selects random things from arrays to make a sentence. It is supposed to pick a random item from objectArray and display it through a div with document.getElementById()

Code

<script>
    var objectArray = ["computer", "iphone", "duck", "candle", "oven", "sofa", "curtain", "blanket", "star", "piano", "turtle", "fluffiness", "door", "pillow", "funny face", "lamp", "healthy heart", "rainbow", "lamp", "clock", "coat", "just luck", "stop sign", "window"];
    var randObject = objectArray[Math.floor(Math.random() * objectArray.length)];

    document.getElementById("Objecto").innerHTML = "randObject";    
</script>

<center>
    <div id="Objecto"></div>
</center>
1
  • What's the question here? Commented Mar 25, 2016 at 2:07

1 Answer 1

1

You're setting the element's innerHTML to the string literal "randObject" instead of the contents of the variable randObject. Use this instead:

document.getElementById("Objecto").innerHTML = randObject;

Here's a complete snippet:

var objectArray = ["computer", "iphone", "duck", "candle", "oven", "sofa", "curtain", "blanket", "star", "piano", "turtle", "fluffiness", "door", "pillow", "funny face", "lamp", "healthy heart", "rainbow", "lamp", "clock", "coat", "just luck", "stop sign", "window"];
var randObject = objectArray[Math.floor(Math.random() * objectArray.length)];

document.getElementById("Objecto").innerHTML = randObject;  
<center>
    <div id="Objecto"></div>
</center>

Sign up to request clarification or add additional context in comments.

Comments

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.