Javascript code works fine ... almost all the time

I ran into a problem that I cannot solve. Check out the code below:

<script> function createFunctions() { var first = ["", "", ""]; var second = ["", "", ""]; var func = ["", ""]; var sign = ["", ""]; for (i = 0; i < 3; i++) { first[i] = (Math.round(Math.random() * 9) + 1); second[i] = (Math.round(Math.random() * 9) + 1); sign[i] = (Math.round(Math.random())); if (sign[i] == "1") { sign[i] = '+'; } else { sign[i] = '-'; } if (first < 2) { func[i] = 'f(x) = x ' + sign[i] + ' ' + second[i] + '<p>'; } else { func[i] = 'f(x) = ' + first[i] + 'x ' + sign[i] + ' ' + second[i] + '<br>'; } } for (i = 0; i < 3; i++) { document.getElementById("createFunctions").innerHTML += 'Function ' + [i + 1] + ': ' + func[i]; } //whichFunction= findAnswers(first, second, sign); } function findAnswers(first, second, sign, rand) { var num = ["", "", ""]; rand = (Math.round(Math.random() * 1)); document.getElementById("findAnswers").innerHTML = 'Which <b>one (or more)</b> of these functions holds true, when plugged in with the following <b>values of x</b>? (' + [rand + 1] + ')<br>'; for (i = 0; i < 3; i++) { num[i] = (Math.round(Math.random() * 9)); } for (i = 0; i < 3; i++) { ans = 0; if (sign[rand] == "+") { ans = [first[rand] * num[i]] + second[rand]; } else { ans = [first[rand] * num[i]] - second[rand]; } document.getElementById("findAnswers").innerHTML += [i + 1] + '. You put in a ' + num[i] + ': ' + ans + '<br>'; } } </script> <BODY onload=createFunctions()> <b>A Machine Called Effex</b> <p><input type="button" value="New Examples" onclick="history.go(0)" VALUE="Refresh"></p> <p id="createFunctions"></p> <p id="findAnswers"></p> 

Everything works great. Unless in the calculation of a function, the code is multiplied by x, and then simply concatenates the second value with the first, instead of adding (or subtracting).

+5
source share
2 answers

You must change [first[rand]*num[i]] to (first[rand]*num[i]) . The bracket [] creates an instance of the array with only one value (the product of your multiplication), and then you add the array to the number, which forces the mechanism to transfer the array to a string and concatenate the string using the number you add.

To illustrate, consider the code below. It also creates an array, but discards it by a number using the unary + operator . This will result in a numerical value, not an array, so your code will work as expected.

 +[first[rand]*num[i]] 

To illustrate what happens, consider the code below. It also creates a singleton array, but by adding it with [0], we specify the (numeric) value of this element and thus the engine will not force the array to a string when using the + operator.

 [first[rand]*num[i]][0] 
+3
source

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.

0
source

Source: https://habr.com/ru/post/1233927/


All Articles