Hey guys im new to javascript and trying to get a better understanding of it, i am trying to make a 8 ball project and currently just to see if it works get it to display in the console but all i recieve is that it is undefined
JavaScript
x
13
13
1
const predictions = ['Ask again later, Better not tell you now, Cannot predict now, Concentrate and ask again'];
2
const btn = document.getElementById('btn')
3
4
btn.addEventListener('click', function() {
5
console.log(predictions[randomNumber])
6
});
7
8
9
10
11
function randomNumber() {
12
return Math.floor(math.random() * predictions.length)
13
};
JavaScript
1
11
11
1
body {
2
background: linear-gradient(to right, #06932d, #000000);
3
display: flex;
4
align-items: center;
5
justify-content: center;
6
}
7
8
#prediction {
9
position: fixed;
10
top: 300px;
11
}
JavaScript
1
24
24
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<link href='8-ball.css'rel="stylesheet">
8
<script defer src="8-ball.js"></script>
9
<title>8 Ball</title>
10
</head>
11
<body>
12
<div id="title">
13
<h1>Try the Magic 8 Ball!</h1>
14
</div>
15
16
<div id="btn">
17
<button>Try Me!</button>
18
</div>
19
20
<div id="prediction">
21
<p>text</p>
22
</div>
23
</body>
24
</html>
Advertisement
Answer
There are a number of issues in your code:
You are missing the parenthesis to call
randomNumber
here:JavaScript121console.log(predictions[randomNumber()]);
2
Math
always starts with uppercase:JavaScript121return Math.floor(math.random() * predictions.length);
2
It looks like you want
predictions
to be an array with multiple string elements. Instead, you have a single element with commas in it:JavaScript121const predictions = ['Ask again later, Better not tell you now, Cannot predict now, Concentrate and ask again'];
2
With everything fixed:
JavaScript
1
18
18
1
function randomNumber() {
2
return Math.floor(Math.random() * predictions.length);
3
}
4
5
const predictions = [
6
'Ask again later.',
7
'Better not tell you now.',
8
'Cannot predict now.',
9
'Concentrate and ask again.'
10
];
11
12
const btn = document.getElementById('btn');
13
const prediction = document.getElementById('prediction');
14
15
16
btn.addEventListener('click', () => {
17
prediction.textContent = predictions[randomNumber()];
18
});
JavaScript
1
39
39
1
body {
2
background: linear-gradient(to right, #06932d, #000000);
3
font-family: monospace;
4
color: white;
5
display: flex;
6
align-items: center;
7
justify-content: center;
8
min-height: 100vh;
9
margin: 0;
10
}
11
12
#title {
13
font-size: 24px;
14
margin: 0;
15
}
16
17
#box {
18
display: flex;
19
align-items: center;
20
justify-content: center;
21
flex-direction: column;
22
}
23
24
#btn {
25
border: 3px solid white;
26
font-family: monospace;
27
background: transparent;
28
padding: 8px 16px;
29
border-radius: 4px;
30
color: white;
31
font-weight: bold;
32
font-size: 24px;
33
margin: 32px 0;
34
}
35
36
#prediction {
37
font-size: 24px;
38
margin: 0;
39
}
JavaScript
1
5
1
<div id="box">
2
<h1 id="title">Try the Magic 8 Ball!</h1>
3
<button id="btn">Try Me!</button>
4
<p id="prediction"> </p>
5
</div>