2

I'm trying to get a random generator of text and images which should be super simple but I cannot for the life of me figure out why it's not working out. Any help is greatly appreciated!

HTML Scroll Demo #1

<body>
    <div id="container">
        <p>Where would you live and what would you drive?</p>
        <p id="text"></p>
    </div>
</body>

CSS

#container {
width: 960px;
margin-left: auto;
margin-right: auto;
border: gray solid;
padding: auto;
background-image: url(../images/house.jpg) no-repeat;
}

#text {
font-size: 40px;
text-align: left;
}

p {
width: 100px;
color: black;
font-family: arial;
font-size: 18px;
text-align: left;
}

JQUERY

$(document).ready(function(){


  var car = ["limo", "golf cart", "pig"];
  var images = ["house.jpg", "dumpster.jpg", "mansion.jpg"];

  var x = images[Math.Floor(Math.random() * images.length)];

    document.getElementById("text").innerHTML=car[x];
  $('html').css({'background-image':'url(images/' + images [x] + ')'});
    });

Thanks in advance!

8
  • Do you see any error in your console? What is the value of x? Commented Mar 25, 2015 at 20:05
  • Uncaught TypeError: undefined is not a function on script.js line 7 which is the var x=images[math.floor(math.random() *images.length)]; Commented Mar 25, 2015 at 20:06
  • Did you try Blazemonger's suggestion? you should have var x = Math.Floor(Math.random() * images.length), not x=images[math.floor(math.random() *images.length)]; Commented Mar 25, 2015 at 20:09
  • yes I responded to them below, it's still not working :( Commented Mar 25, 2015 at 20:09
  • Please try to use console.log(x) and tell me the result. Commented Mar 25, 2015 at 20:11

1 Answer 1

4

You appear to expect x to be a number, not an image:

var x = Math.floor(Math.random() * images.length); // not Floor
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your response. I did change and it's still not behaving accurately. :(
Oh my god thank you so much. I can't believe that was the problem. You're the best!!

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.