0
votes

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speed typing game</title>
    <script src="speed.js" defer></script>
    <link rel="stylesheet" href="speed.css">
</head>
<body>
    <input type="button" value="Start game!" class="button">
    <div class="letter">A</div>
    <div></div>

</body>
</html>

how i solve this error it's at line 59 so you know where it is do i have to give a value to the letter i tried that but it didn't work? speed.js:59 Uncaught TypeError: Cannot set property 'top' of undefined at speed.js:59 ```let max=10 var letter= 'A' let color="purple" let right='right' var top='top' var startbutton=document.querySelector('input[type=button]')

        startbutton.addEventListener("click",startgame)
// start the game with a random number,hide the button//
        function startgame() {
            
            startbutton.classList.add("hidden")
            let interval = 1000
            setInterval(createNewLetter,interval);
}
/*generate random number between 1-8 */
function randomNumber(min,max) {
 return Math.floor(Math.random() * ( max+ 1))
}
// generate a random letter //
function randomLetter() {
 //generam un cod Ascii intre 65('A') si 90('Z') //
        let codeofA= "A".charCodeAt(0);
 let codeofZ= "Z".charCodeAt(0)       
 let randomCode= randomNumber(codeofA, codeofZ);
        // convertim codul ascii in caracterul asociat lui //
        return String.fromCharCode(randomCode)

}

// genereaza o culoare aleatorie //
 function randomColor() {
         let red = randomNumber(0,255)
         let green = randomNumber(0,255)
         let blue= randomNumber(0,255)
         return 'rgb(${red},${green},${blue})'
 }
 // genereaza o pozitie aleatorie intre 0% si 90% //
 function randomPosition() {
         let position = randomNumber(0,90)
         return '${position}%'
 }
 // creaza un nou element div cu o litera aleatorie //
 // adaugarea elementului in html//
 function createNewLetter() {
         let letter = randomLetter()
         let color= randomColor()
         let top = randomPosition()
         let right= randomPosition()
 }
 // cream un nou element div //
 let div = document.createElement("div")
 // adaugam o clasa letter //
 div.classList.add("letter")
 //adaugam un nou stil si o noua culoare literei //
 div.style.backgroundColor = randomColor
 // adaugam o pozitie literei //
 div.Style.top = top
 // pozitionam litera //
 div.Style.right = right
// cream un element body // 
 document.querySelector("body").appendChild(div)
1

1 Answers

0
votes

Update:

Need to replace

div.classlist.add("letter")

with

div.classList.add("letter")

The error is saying that whatever element you are trying to add to, on line 54, is undefined.

Looking at your code, it's either startbutton or div, those are the only elements that have "add" associated with it.

Can you post the full file so that we can see what's at line number 54?