I just failed in a JavaScript test and I would really appreciate some comments of how I can improve so I can keep learning.
The test was about making a form interactive, where the code should do things like changing to the next tab while showing not visible content, validate email, etc.
I have received the following feedback to consider:
[-]
caching (no jquery selector is cached)----
[-]
performance optimization (nope, usage of jquery each instead of a native loop)---
[-]
reusable code (hardly, as commented above)---
[+]
clean code and good structure---
[ ]
Extra points for applied design pattern
$("<link rel='stylesheet' href='css/ui-lightness/jquery-ui-1.10.1.custom.css' type='text/css' media='screen' />").insertAfter("[type='text/css']"); // CSS de Jquery UI
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};
function setTabOn(stepNumber) {
stepNumber = parseInt(stepNumber);
$("#step" + stepNumber + "_tab").addClass("on");
$("#step" + stepNumber + "_tab").click(function() {
$(".step").hide();
$("#step" + stepNumber).show();
$("#steps a.active").removeClass("active");
});
}
function nextStep(stepNumber) {
stepNumber = parseInt(stepNumber) + 1;
$(".step").hide();
$("ol#steps li a").removeClass("active");
$("#step" + stepNumber).show();
$("#step" + stepNumber + "_tab").addClass("active");
}
$(document).ready(function() {
$(".back").click(function() {
var idBack = $(this).attr("id");
var idBack = idBack.match(/\d/) - 1;
$(".step").hide();
$("#step" + idBack).show();
});
$("#step2_confirm_button").click(function() {
$("#step2_confirm").dialog("destroy");
nextStep(2);
setTabOn(2);
});
$("#step2_cancel_button").click(function() {
$("#step2_confirm").dialog("destroy");
});
$(".step").hide();
$("#step1").show();
$("#step1_tab").addClass("active");
$("#step2_confirm").hide();
$("#step1_next").click(function() {
var flagFirstStepError = 0;
var flagFirstStepEmpty = 0;
var flagFirstStepEmail = 0;
$("#step1_errors").html("");
$(".required").each(function() {
if ($(this).val() == "") {
flagFirstStepError = 1;
return false
}
else {
flagFirstStepEmpty = 1;
}
}); // end each
if (flagFirstStepError == 1) {
$("#step1_errors").append("We are sorry, but you need to enter all the required fields in order to proceed. The following information is missing: <br /><ol>");
$(".required").each(function() {
if ($(this).val() == "") {
var labelId = $(this).attr("id");
var y = $("#step1 label[for=" + labelId + "]").html();
$("#step1_errors").append("<li>You must fill the " + y + " field</li>");
}
}); // end each
$("#step1_errors").append("</ol>");
}
var userEmail = $("#email").val();
if (!isValidEmailAddress(userEmail)) {
$("#step1_errors").append("<p> You must provide a valid email adress");
flagFirstStepEmail = 0;
}
else {
flagFirstStepEmail = 1;
}
if (flagFirstStepEmail + flagFirstStepEmail == 2) {
nextStep(1);
setTabOn(1);
}
}); //end click
$("#step2_next").click(function() {
$("#confirm_list li").remove();
$("#share_list li label").find("input:checked").each(function() {
var x = $(this).val();
$("#confirm_list").append("<li>" + x + "</li>");
});
$("#step2_confirm").dialog();
});
});