Stack Overflow Asked by WiseEye on November 12, 2021
How can I add operation symbols (+, *, -, /
) to an array in JavaScript? I am trying to get a randomized symbol from an array and I have 4 options: +, *, -, /.
var operations = [*, +, -, /]
It does not detect it as an array of symbols though.
I am trying to create a webapp for me to practice math. I want to get two random numbers that will get operated (added, multiplied, divided, or subtracted) by a random operation (-, /, *, or +). so the equation becomes totally random, example: 472 * 820 =.
The simple answer to "How can I add operation symbols (+, *, -, /) to an array in Javascript" is that you can't do that.
An operator is a symbol in the language, but it is not a string, number, boolean, or an Object, so it can't be part of a data structure.
You can put some kind of representation of an operator in an array, such as a string:
let operations = [ '*', '+', '-', '/' ];
There are several pretty good answers, and with your added explanation of your goal I would recommend something that, in some ways, combines some elements from those answers.
You want to display two random numbers with the choice of a random operation to create a math problem. Displaying them suggests using string representations of the operators.
However, you also want to perform the operation that the operator represents; that means you have to turn that operator string into a computation in some way. One way is to switch
on the chosen operator, similar to what hev1 does...
let num1 = 839;
let num2 = 195;
let op = '+';
let answer;
switch (op) {
case '*':
answer = num1 * num2;
break;
case '/':
answer = num1 / num2;
break;
case '+':
answer = num1 + num2;
break;
case '-':
answer = num1 - num2;
break;
}
console.log('answer is', answer);
But now your array and your computation code have to be kept synchronized.
You can also create a function array like 8HoLoN does, but then you have no symbol to display in the page unless you again have a corresponding way to pick both the symbol to display and the operation to perform.
I would create an array of objects, where each object contains the symbol to display and a function to perform the operation.
Look in particular at how the answer is calculated at let answer = ...
and how the operator is displayed in the console.log
line.
// Here's your array of operators. Each object in the array has
// • an "operator" symbol
// • a calculator to perform that operation.
const operators = [
{
'operator' : '*',
'calc' : (a, b) => a * b
},
{
'operator' : '/',
'calc' : (a, b) => a / b
},
{
'operator' : '+',
'calc' : (a, b) => a + b
},
{
'operator' : '-',
'calc' : (a, b) => a - b
},
];
let num1 = 296;
let num2 = 138;
let op = operators[getRandomIntInclusive(0, operators.length - 1)];
let answer = op.calc(num1, num2);
console.log(num1, op.operator, num2, '=', answer);
/* random int function from MDN
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values_inclusive
*/
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Answered by Stephen P on November 12, 2021
You can't. *
/
+
-
are operators of the JS language and then you can't reference them as objects in a array.
If what you want is make dynamic random operation, you should use function.
const operators = [
(a, b) => a + b,
(a, b) => a * b,
(a, b) => a - b,
(a, b) => a / b
];
const a = 2, b = 3;
const rand = Math.floor(Math.random() * 3.9);// between [0;4[
const res = operators[rand](a, b);
console.log(res);
Answered by 8HoLoN on November 12, 2021
You simply need to add quote around the symbols as follows -
var operations = ['*', '+', '-', '/'];
console.log(operations);
function getRandom(){
return operations[Math.floor(Math.random() * Math.floor(4))]
}
// To get a random symbol -
console.log(getRandom())
// One way to use a random symbol in a given expression would be as -
const num1 = Math.floor(100*Math.random()), num2 = Math.floor(100*Math.random());
const expression = `${num1}${getRandom()}${num2}`
const result = eval(expression)
console.log(result)
As you can see, you can use eval()
, if you are forming an expression string using the way I have shown in the above code snippet. The eval()
function evaluates JavaScript code represented as a string.
Answered by Abhishek Bhagate on November 12, 2021
You need quotes around strings.
var operations = ['*', '+', '-', '/'];
var randIdx = Math.random() * operations.length | 0;
var randOp = operations[randIdx];
console.log(randOp);
To actually perform random operations, you do not need the operators themselves: you can just generate a random integer from 0-3 and map each integer to an operation yourself.
var rand1 = Math.random() * 50 | 0 + 50,
rand2 = Math.random() * 50 | 0 + 50,
randOp = Math.random() * 4 | 0;
console.log(rand1, ["+", "-", "*", "/"][randOp], rand2, "=");
switch(randOp){
case 0:
console.log(rand1 + rand2);
break;
case 1:
console.log(rand1 - rand2);
break;
case 2:
console.log(rand1 * rand2);
break;
default:
console.log(rand1 / rand2);
}
Answered by Unmitigated on November 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP