Hello folks!
I’m completely new to JavaScript and programming altogether. I’ve got a little experience in VBscript and that’s it, nothing special.
I’m armed only with the books that I read and with a lot of good will to learn something new.
So by now I have a basic knowledge of VBscript, HTML, CSS and JavaScript. I gave my self a task to write a simple script to test what I have
been learned so far.
My question is what do you think about the script that I have been posted here, the way I wrote it… Ok, I know that there is probably
a million of different ways to achive the same result, but… I want you to pay special attention to the FUNCTION section. What do you think about it?
Is it too complicated, too hard to follow the code??? ? I didn’t finished it yet but that is not important ?
Thanks in advance!
This is the script:
//————————————————————————-INITIALIZATION SECTION—————————————————————————————————–
var blnFirstCheck = “false”;
var blnSecondCheck = “false”;
var blnThirdCheck = “false”;
var blnFourthCheck = “false”;
var blnTryAgain = “false”;
var blnWhileLoop = “true”;
var blnInnerWhileLoop = “true”;
var strMathOperation;
var strUserChoice;
var strToConc;
var intNumber;
var result;
//————————————————————————MAIN PROCESSING SECTION———————————————————————————————————
while (blnWhileLoop != “false”)
{
blnFirstCheck = “true”;
strUserChoice = prompt(“Select one of the options:” + “n” + “1.String concatenation” + “n” + “2.Mathematical operations”, “”);
checkUserInput(strUserChoice);
strUserChoice = Number(strUserChoice);
blnFirstCheck = “false”;
}
switch (strUserChoice)
{
case 1:
{
blnSecondCheck = “true”;
result = “”;
blnWhileLoop = “true”;
while (blnWhileLoop != “false”)
{
strToConc = prompt(“Enter one or more characters to concatenate together. To display final result, leave an input field empty and press OK button.”, “”);
checkUserInput(strToConc);
result = result + strToConc;
blnSecondCheck = “false”;
}
break;
}
case 2:
{
result = “”;
blnWhileLoop = “true”;
while (blnWhileLoop != “false”)
{
blnThirdCheck = “true”;
intNumber = prompt(“Enter a number: “, “”);
blnTryAgain = checkUserInput(intNumber);
if (blnTryAgain == “true”)
continue;
else
blnTryAgain = “false”;
intNumber = Number(intNumber);
blnThirdCheck = “false”;
while (blnInnerWhileLoop != “false”)
{
blnFourthCheck = “true”;
strMathOperation = prompt(“Select one of four possible math operations to perform.” +
“For addition ‘+’, subtraction ‘-‘, multiplication ‘*’, division ‘/’.”, “”)
checkUserInput(strMathOperation);
blnFourthCheck = “false”;
}
}
}
}
if (result == undefined)
result = “”;
document.write(“<p>” + “Result is: ” + “<span>” + result + “</span>” + “</p>”)
//—————————————————————————–FUNCTIONS————————————————————————————————————
function checkUserInput(strToCheck)
{
try
{
if (strToCheck == null || strToCheck == “”)
throw “See you!”;
if (isNaN(strToCheck) == true && blnSecondCheck != “true” && blnFourthCheck != “true”)
throw “Only numbers are allowed, please try again.”;
else
if (blnFourthCheck == “true” && isNaN(strToCheck) == false)
throw “Please select one of for possible operators.”
if ((strToCheck.length > 1) && (blnFirstCheck == “true” || blnFourthCheck == “true”))
throw “Please select only one option at a time.”;
if ((blnFourthCheck == “true”) && (strToCheck != “+” && strToCheck != “-” && strToCheck != “*” && strToCheck != “/”))
throw “Please select one of for possible operators.”
if ((blnSecondCheck == “true” || blnThirdCheck == “true”) && (strToCheck != null || strToCheck != “”))
blnWhileLoop = “true”;
if (blnFourthCheck == “true”)
blnInnerWhileLoop = “false”;
else
blnWhileLoop = “false”;
}
catch(error)
{
if (error == “See you!” || error == “”)
if (blnFirstCheck == “true”)
{
alert(error);
blnWhileLoop = “false”;
}
else
blnWhileLoop = “false”;
else
{
alert(error);
if (blnThirdCheck == “true”)
return “true”;
}
}
return;
}