Mark Mark - 1 year ago 103
AngularJS Question

Compare values from array

Im taking random values from array A and B and trying to compare their solved values, is not working becaues values in arrays are strings, how to get them to number and solve them?

.controller('questions', function($scope) {

var A = ['6-2', '7+3', '8*1', '9/3', '8+1'];

var B = ['1+5', '7-3', '10-5', '10/2', '3*2'];

var questionA = A[Math.floor(Math.random()*A.length)];

var questionB = B[Math.floor(Math.random()*B.length)];

if(questionA > questionB ){

console.log('It's bigger');

console.log('It's smaller');

Answer Source

You can use $scope.$eval() to get the result of the equation:

var answerA = $scope.$eval(questionA),
    answerB = $scope.$eval(questionB);

if(answerA > answerB)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download