0

This is the error I'm getting on my console: VM1007:1 Uncaught ReferenceError: getRandomColor is not defined at :1:1

It is saying my function is not defined. Can anyone tell me what I can do to fix this error?

home.js

var color = getColor(6);


var squares = document.querySelectorAll(".square");
//Selects the color you have to pick
var colorSelected = colorSelected();
var randomColor = document.getElementById("randomColor");
var showResponse = document.getElementById("response");
randomColor.textContent = colorSelected;
//add colors to squares
for (var i = 0; i < squares.length; i++){
    squares[i].style.backgroundColor = color[i];
    //adds click listeners to squares

    squares[i].addEventListener("click", function(){
        //grab color and compare it to colorSelected
        var clicked = this.style.backgroundColor;
        if (clicked === colorSelected){
            showResponse.textContent ="correct!";
        } else{
            //fades out so wrong answer disappears
            this.style.backgroundColor = "black";
            showResponse.textContent = "oops, try again!";
        }
    });


}

function colorSelected(){
    var random = Math.floor((Math.random(color) * 6) + 1);
    return color[random];
}

function getColor(num){
    //make an array unsure to what to name it yet
    var arr = []
    for (var i = 0; i < num; i++){

    }
    //return the array
    return arr;

}

//picks a random color from 0 - 255
function getRandomColor(){
    var rgb1 = Math.floor((Math.random() * 255) + 1);
    var rgb2 = Math.floor((Math.random() * 255) + 1);
    var rgb3 = Math.floor((Math.random() * 255) + 1);
    return "rgb(rgb1 + rgb2 + rgb3)";


}

I'm not sure what the issue here so some help will be greatly appreciated.

  • Your script probably didn't load for whatever reasons. Note that this getRandomColor will always return the **string** `"rgb(rgb1 + rgb2 + rgb3)"`, probably not what you want. – Kaiido Oct 05 '18 at 05:52
  • I'm trying to return the 3 variables in that function. How would I go about doing that then? –  Oct 05 '18 at 05:54
  • https://stackoverflow.com/questions/31845895/whats-the-best-way-to-do-string-building-concatenation-in-javascript – Kaiido Oct 05 '18 at 05:56
  • For returning values of color from getRandomColor function, you need to change line from: return "rgb(rgb1 + rgb2 + rgb3)"; to: return "rgb("+ rgb1 +","+ rgb2 +","+ rgb3 +")"; – Prasad Wargad Oct 05 '18 at 06:03
  • That is still not returning anything –  Oct 05 '18 at 06:09

1 Answers1

0

The getColor() function returns an empty array. You might want to push some elements in the array.

function getColor(num){
    //make an array unsure to what to name it yet
    var arr = [];
    for (var i = 0; i < num; i++){
        arr.push(getRandonColor());
    }
    //return the array
    return arr;
}

Also in your getRandomColor() function the return value is wrong. It should be of the form

function getRandomColor(){
    var rgb1 = Math.floor((Math.random() * 255) + 1);
    var rgb2 = Math.floor((Math.random() * 255) + 1);
    var rgb3 = Math.floor((Math.random() * 255) + 1);
    return "rgb("+ rgb1 + "," +  rgb2 + "," + rgb3 +")";       //Since rgb1, rgb2 and rgb3 are variables, append its value to the string
}
dreamHatX
  • 479
  • 3
  • 9