Please help the green beginner beginner. I am programming a home calculator in js (so I'm not afraid of the terrible eval()) I fixed all (I hope so) errors, but unfortunately there is one that is difficult to fix (if at all possible), in general the eval() method in js and python thinks that: -1 ^ 0.5 = -1 (second way square root) how to fix this ??
found what the main problems were:
1st - it was impossible to write -1 ^ 0.5 (the (^) sign is not supported, only the (**) sign) - you need to indicate this in the instructions
2nd - it was impossible to write -1 ^ 0.5 ("-1" cannot be written, negative numbers must be enclosed in brackets (-1))
3rd - there was an error in this line of code: result = Math.round ((result + Number.EPSILON) * 100) / 100; it should be like this: result = Math.round (result * 100) / 100; (some way of rounding to two decimal places)
I need to do the following:
• you need to complete the error message (alert - "You entered -1 instead of (-1)"
• indicate in the instructions that you cannot enter alphabetic and special characters
• sign (^) - not supported, only sign (**) - to raise a number to a power
• "-1" cannot be written, negative numbers must be enclosed in parentheses (-1)
I am explaining because it will be useful to someone too.
So with eval () everything works as it should, thanks to @GenericUser he directed me in the right direction to find the error, where I started digging after.
here is the code:
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// Calculating a mathematical expression
// Input mathematical expression (logic for text-box)
function mathematical_expression() {
$(document).ready(function() {
$('#secret_box').dialog({
modal: true,
autoOpen: true,
buttons: {
'Cancel': function() {
$(this).dialog('close'); },
'Accept': function() {
$('#mainForm input#target').val($(this).find('#secret_widget').val());
console.log('The «Accept» is pressed!-[1.14]');
$(this).dialog('close'); } } });
$('#button14').click(function() {
$('#mainForm input#target').val($(this).find('#secret_widget').val());
console.log("math expression = " + math_expression1);
console.log("math expression = " + typeof(math_expression1));
console.log('The «Accept» is pressed!-[2.14]');
// $('#secret_box').dialog('close');
}); }); };
// Binding the «Accept» button to the «Enter» key (14)
function clickPress14(event) {
if (event.keyCode == 13 && math_expression1 == $('#secret_widget').val()) {
$('#mainForm input#target').val($(this).find('#secret_widget').val());
console.log("math expression = " + math_expression1);
console.log("math expression = " + typeof(math_expression1));
console.log("The button «Enter» is pressed!-[3.14]");
$('#secret_box').dialog('close');
try {
result = eval(math_expression1);
result = Math.round((result + Number.EPSILON) * 100) / 100; } // more correct rounding to two decimal places
catch (SyntaxError) {
Syntax_Error();
console.log('Catch Error!'); }
console.log("result = " + result);
console.log("result = " + typeof(result));
super_dialog(); };
if (event.keyCode == 13 && math_expression1 != $('#secret_widget').val()) {
console.log("math expression = " + math_expression1);
console.log("math expression = " + typeof(math_expression1));
console.log("The button «Enter» is pressed!-[3.14]"); }; }
// value_immediately14
function value_immediately14() {
$(document).ready(function() {
math_expression1 = $('#secret_widget').val();
console.log("math expression = " + math_expression1);
console.log("math expression = " + typeof(math_expression1));
$('#button14').click(function() {
$('#mainForm input#target').val($(this).find('#secret_widget').val());
$('#secret_box').dialog('close');
try {
result = eval(math_expression1);
result = Math.round((result + Number.EPSILON) * 100) / 100; } // more correct rounding to two decimal places
catch (SyntaxError) {
Syntax_Error();
console.log('Catch Error!'); }
console.log("result = " + result);
console.log("result = " + typeof(result));
super_dialog(); }); }); }
// Secret result
function super_dialog() {
$(document).ready(function() {
alert("The result of your math expression: " + math_expression1 + " will be is - " + result);
function alert(alert_text) {
$("#super_dialog").show();
$("#secret_text").text(alert_text); }
$("#secret_close").click(function() {
$("#super_dialog").hide();
window.location.reload(); }); }); }
// Syntax_Error Alert!
function Syntax_Error() {
$(document).ready(function() {
alert("The characters you entered are completely"
+ "\ndifferent from the mathematical expression."
+ "\nSo come on already bro... Adios mi amigo!!!");
function alert(alert_text) {
$("#alert_dialog10").show();
$("#alert_text10").text(alert_text); }
$("#alert_close10").click(function() {
$("#alert_dialog10").hide();
window.location.reload(); }); }); }
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// Warning! Incorrect input!
//Warning! Incorrect input!
function warning() {
if (sum == 0 || period == 0 || percent == 0) {
alert_result8(); }
else if (sum == 0 && period == 0 && percent == 0) {
alert_result8(); }
else if (X == 0 || Y == 0 || P == 0) {
alert_result8(); }
else if (X == 0 && Y == 0 && P == 0) {
alert_result8(); }
else if (Z == 0 && N == 0) {
alert_result8(); } }
// Warning! Incorrect input! (alert-box)
function alert_result8() {
$(document).ready(function() {
alert("You entered either the first or the second number incorrectly,"
+ "\nor you entered both numbers incorrectly at once! You may"
+ "\nalso have entered zero or both numbers as zeros!");
function alert(alert_text) {
$("#alert_dialog8").show();
$("#alert_text8").text(alert_text); }
$("#alert_close8").click(function() {
$("#alert_dialog8").hide(); }); }); }
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//