You use square brackets where you should have used parentheses.
Square brackets are used to create arrays. For example, [2 * 3] creates an array that contains one element: 6. In addition, the + operator will perform numerical addition or concatenation of strings, depending on the operand. Adding an array and a number leads to concatenation. So this statement, for example:
ans = [2 * 3] + 4;
Calculated as:
ans = "6" + "4"; // string "64"
You need to get rid of the square brackets that are used incorrectly.
Other than that, you are also using the Math.random() function incorrectly. This statement, for example:
Math.round(Math.random() * 9) + 1
Appears to return a random number from 1 to 10. However, the numbers 1 and 10 will be less likely than the numbers from 2 to 8. The correct way to generate a random number between min and max is
Math.floor(Math.random() * (max - min + 1)) + min
Finally, this statement:
rand = Math.round(Math.random() * 1);
Only 0 and 1 will be returned, but not 2. You also need to fix this.
source share