What is the error? [closed]

-3

The document does not change color

function changeme() {
	 
	 var color = "#";
	 var letras = "0123456789ABCDEF";
	 function random() {
	 for (var i=0; i<6; i++) {
		 
		 color += letras[Math.floor(Math.random() * 16);
		 
	 }
	 
	 return color;
	 }
	 
	 function setcolor() {
		 
		 document.getElementById("changeme").setAttribute("style", "color:" random(););
		 
	 }
	 return random();
	 
 }
 
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="learn.js"></script>
  <style>
    #forEach {width: 300px; height: 100px; background-color: red; position: relative;}
	#forClasico {width: 300px; height: 100px; background-color: blue; position: relative;}
	#forin {width: 300px; height: 100px; background-color: green; position: relative;}
	#changeme {width: 300px; height: 100px; background-color: green; position: relative;}
  </style>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<!--  <div id="forEach">
        <h4 style="text-align:center;">Simplex forEach LOOP(array start w/ 1)</h4>
		
    </div>
	<div id="forClasico">
        <h4 style="text-align:center;">Simplex for LOOP(1 - 10)</h4>
		
    </div>
	<div id="forin">
        <h4 style="text-align:center;">Simplex forin(at the moment in array)</h4>
    </div>--->
	<div id="changeme" onclick="changeme()">
        <h4 style="text-align:center;">Changing the bgcolor(click to change for random color);</h4>
    </div>
</body>
</html>
    
asked by Eduardo Sebastian 30.05.2017 в 20:08
source

2 answers

2

Apart from the compilation errors, you also need to call the method setcolor , in the javascript code you only return the function random . Before return you should call the method setcolor() I also see errors in the function setcolor I suggest this code for javascript

function changeme() {
var color = "#";
var letras = "0123456789ABCDEF";
function random() {
    for (var i=0; i<6; i++) {
        color += letras[Math.floor(Math.random() * 16)];
    }
    return color;
}

function setcolor() {
    document.getElementById("changeme").style.color = random();
}
setcolor();
}
    
answered by 30.05.2017 / 20:46
source
1

You had two compilation problems: the ; in random();); and you need to close ] in letras[Math.floor(Math.random() * 16);

function changeme() {

    var color = "#";
    var letras = "0123456789ABCDEF";

    function random() {
        for (var i = 0; i < 6; i++) {
            color += letras[Math.floor(Math.random() * 16)];
        }

        return color;
    }

    function setcolor() {
        document.getElementById("changeme").setAttribute("style", "color:" + random());
    }
    return random();
}

console.log(changeme());
    
answered by 30.05.2017 в 20:15