I've written it for an assessment. It works but my assessment also requires a peer to review it in terms of how it could be improved, how it rates as a piece of JavaScript, etc, any comments would be appreciated (I don't know anyone else to ask).
As a bit of background this is JavaScript written to be embedded in a HTML doc. It calculates the balance of two accounts when they each increment daily by a certain value. It reports when they will be equal, when the lesser balance exceeds the greater, or if the accounts will always be unequal. All values can be entered by a user.
I look forward to your comments.
/process function is called by user pressing a Process button/
function process()
{
var account1 = document.getElementById("account_1").value;
var account2 = document.getElementById("account_2").value;
var increm1 = document.getElementById("increm_1").value;
var increm2 = document.getElementById("increm_2").value;
blankOrSame(account1,account2,increm1,increm2);
account1 = Number(account1);
account2 = Number(account2);
increm1 = Number(increm1);
increm2 = Number(increm2);
alwaysDiff(account1,account2,increm1,increm2);
sameGreater(account1,account2,increm1,increm2);
}
/*field_vldn function is performed onchange when user updates fields*/
function field_vldn(name) //onchange validation for account & increment fields.
{
var num = document.getElementById(name).value;
if ( isNaN(num))
{
alert("Please enter a number.");
return document.getElementById(name).value = null;
}
}
/below functions are called by process function/
function blankOrSame(account1,account2,increm1,increm2)//validation for blank fields or if accounts will always be equal.
{
if (account1 == "" || account2 == "" || increm1 == "" || increm2 == "")
{
alert("Please complete all fields then press the 'Process' button.");
}
else if (account1 == account2 && increm1 == increm2)
{
alert("Based on the values entered the both Accounts will always have an equal balance.");
}
}
function alwaysDiff(account1,account2,increm1,increm2)//determined if accounts will always be inequal. Variables must first be converted to numbers.
{
if ((account1 <= account2 && increm1 < increm2) || (account1 < account2 && increm1 == increm2))
{
alert("The accounts will never be equal. Account 1 will always contain less money than Account 2.");
}
else if ((account1 >= account2 && increm1 > increm2) || (account1 > account2 && increm1 == increm2))
{
alert("The accounts will never be equal. Account 2 will always contain less money than Account 1.");
}
}
function sameGreater(account1,account2,increm1,increm2)//determines when accounts will be same or lesser exceeds greater.
{
if (account1 < account2 && increm1 > increm2)//statement for when account 1 is initally lesser.
{
var n = 0;
while (account1 < account2)
{
account1 = account1 + increm1;
account2 = account2 + increm2;
n = ++n
}
if (account1 == account2)//accounts eventually become equal, will also report when account1 exceeds account2.
{
alert("After " + n + " day/s Account 1 and Account 2 will be the same balance ($" + account1 + ").\nIn "
+ (n+1) + " day/s Account 1 ($" + (account1+increm1) + ") will be greater than Account 2 ($" + (account2+increm2)
+ ").");
}
else if (account1 > account2)//account1 exceeds account2 without ever becoming equal.
{
alert("After " + n + " day/s Account 1($" + account1 + ") will be greater than Account 2 ($" + account2 + ").");
}
}
else if (account1 > account2 && increm1 < increm2)//statement for when account 2 is initally lesser.
{
var n = 0;
while (account1 > account2)
{
account1 = account1 + increm1;
account2 = account2 + increm2;
n = ++n
}
if (account1 == account2)//accounts eventually become equal, will also report when account2 exceeds account1.
{
alert("After " + n + " day/s Account 1 and Account 2 will be the same balance ($" + account1 + ").\nIn "
+ (n+1) + " day/s Account 2 ($" + (account2+increm2) + ") will be greater than Account 1 ($" + (account1+increm1)
+ ").");
}
else if (account1 < account2)//account2 exceeds account1 without ever becoming equal.
{
alert("After " + n + " day/s Account 2 ($" + account2 + ") will be greater than Account 1 ($" + account1 + ").");
}
}
}
document.getElementById
may not work on all of the js enabled browsers. See e.g. this Q/A: stackoverflow.com/questions/4069982/… \$\endgroup\$if (account1 == "" || account2 == "" || increm1 == "" || increm2 == "")
is a bad way to check the fields. What if the user inputs something other than a number? e.g., an space char? \$\endgroup\$