var problems = [
".1+.1+.1+.1+.1+.1+.1", //0.7
".1*.1", //0.01
".1-.1", //0
".1/.1", //1
".1*.1+.1" //0.11
];
//a change
var hasPlusOrMinus = /[\+\-]+/;
var fullNumber = /(\d*\.?\d+|\d+)/g;
var $console = jQuery("#console");
for(var i=0,ii=problems.length; i < ii; i++){
if(hasPlusOrMinus.test(problems[i])){
var newProblem = problems[i].replace(fullNumber, "($1*1000000000)");
//This eval needs to be replaced with something else, it causes problems for mixed calculations (those with both +\- and *\/). Need to do each +\- in the adjusted way, and then maybe eval the rest.
var wrongAnswer = eval(newProblem);
var rightAnswer = wrongAnswer/1000000000;
$console.append(rightAnswer + "<br>");
}else{
var answer = eval(problems[i]);
$console.append(answer + "<br>");
}
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.