0

I am trying to change a grid of small square divs to a random background color. As far as I can tell my syntax is correct and the console is not throwing any errors but it's like the .css() will not accept my genColor method.

I found this which is very similar but the solution offered has not seemed to work.

function genColor() {
'use strict';
var hexes = '0123456789ABCDEF'.split('');
var color = '#';

for (var i = 0; i < 6; i++){
    color += hexes[Math.floor(Math.random * 16)];
}//end for loop

return color;

which is used in this

function highlightSquare() {
'use strict';

$('.square').on('mouseenter', function () {
    $(this).css('background', genColor()); 
});

Please help I can't figure out why this isn't working when examples show me it should.

P.S. The 'use strict'; keeps the Brackets editor JSLint from yelling at me. As near as I can tell it does not affect the code above.

2
  • Is the event firing? Add alert(color); right before the return to ensure it's running and generating a valid color. From what it looks like, your event listener is within the highlightSquare function - where it should probably not be within a function (aside from $(document).load(function(){ });). Commented Apr 5, 2016 at 22:57
  • "#undefinedundefinedundefinedundefinedundefinedundefined" - looks like i found OPs problem Commented Apr 5, 2016 at 23:02

1 Answer 1

4

Looks like the issue is with Math.random, which is a function, so it should be Math.random(). Also, not sure from your code, but the event listener should be assigned outside of a function.

function genColor() {
  'use strict';
  var hexes = '0123456789ABCDEF'.split('');
  var color = '#';
  for (var i = 0; i < 6; i++){
    color += hexes[Math.floor(Math.random() * 16)];
  }//end for loop
  return color;
}

$('.square').on('mouseenter', function () {
  $(this).css('background', genColor()); 
});

Here's a working demo: https://jsfiddle.net/fb15L86u/

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

3 Comments

son of a... I was like what's going on here...nice catch
Wow... I feel incredibly dumb for missing that. Thank you. But can you explain why the event listener should be outside the function? I was intending it to be resusable inclusive of the mouseenter?
@JScharfenberger - Technically you could put it inside a function, and ensure the function is called in your code prior to needing the event attached; but it's easier to tell when it is being listened to if it's just setup from the get-go. If you have a control on the page that determines whether the squares will be colored on hover (turning on coloring squares), you could just evaluate whether that control is enabled in your event listener - that way there's no binding or un-binding the event if you need it turned off.

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.