This is my code, a simple sequel of function were I generate two number, one for the user, one for the PC and who scores the highest number win the game. Firefox has come out with Uncaught SyntaxError: unexpected token: string literal error, I checked my code and everything seems ok to me, I can’t figure out what’s wrong and generates that error
JavaScript
x
34
34
1
// Generate a random number between 1 and 6 both for user and PC.
2
// Who does the highest score win.
3
4
//I create the random number for user and PC
5
var userNumber = getRandomNumber(1, 6);
6
var pcNumber = getRandomNumber(1, 6);
7
8
console.log(userNumber);
9
console.log(pcNumber);
10
11
//With highestScore function the winner comes out
12
var whoWon = highestScore(userNumber, pcNumber);
13
console.log(whoWon);
14
15
//I use this function to obtain the random number
16
function getRandomNumber(min, max) {
17
return Math.floor(Math.random() * (max - min + 1) ) + min;
18
}
19
20
//Function highestScore tell who's won the game
21
//matchMessage tells how the winner or the eventual tie has come
22
//The return is obviously matchMessage
23
function highestScore (num1, num2) {
24
var matchMessage = 'Your number is ' + num1 ', PC number is ' + num2 ', tie!!';
25
26
if (num1 > num2) {
27
matchMessage = 'Your number is ' + num1 ', PC number is ' + num2 ', congrats you've won';
28
} else if (num1 < num2) {
29
matchMessage = 'Your number is ' + num1 ', PC number is ' + num2 ', you lost...';
30
}
31
32
return matchMessage;
33
}
34
Advertisement
Answer
You are missing a plus
+
sign while adding the strings with variables.
What you are doing:
JavaScript121'Your number is ' + num1 ', PC number is '
2
What it should be:
JavaScript121'Your number is ' + num1 + ', PC number is '
2
When you are using the same type of quote in a string then you have two ways to correct it:
Use different strings, like:
JavaScript121", congrats you've won"
2
Or you can escape that string using
, Like
JavaScript121', congrats you've won'
2
Try this:
JavaScript
1
33
33
1
// Generate a random number between 1 and 6 both for user and PC.
2
// Who does the highest score win.
3
4
//I create the random number for user and PC
5
var userNumber = getRandomNumber(1, 6);
6
var pcNumber = getRandomNumber(1, 6);
7
8
console.log(userNumber);
9
console.log(pcNumber);
10
11
//With highestScore function the winner comes out
12
var whoWon = highestScore(userNumber, pcNumber);
13
console.log(whoWon);
14
15
//I use this function to obtain the random number
16
function getRandomNumber(min, max) {
17
return Math.floor(Math.random() * (max - min + 1)) + min;
18
}
19
20
//Function highestScore tell who's won the game
21
//matchMessage tells how the winner or the eventual tie has come
22
//The return is obviously matchMessage
23
function highestScore(num1, num2) {
24
var matchMessage = 'Your number is ' + num1 + ', PC number is ' + num2 + ', tie!!';
25
26
if (num1 > num2) {
27
matchMessage = 'Your number is ' + num1 + ', PC number is ' + num2 + ', congrats you've won';
28
} else if (num1 < num2) {
29
matchMessage = 'Your number is ' + num1 + ', PC number is ' + num2 + ', you lost...';
30
}
31
32
return matchMessage;
33
}