Every topic starts at New. Sign in and your practice counts.
Large programs fail in small places. This topic shows how to check each small unit with tests and how to pause a running page to inspect its values.
This topic teaches unit tests that check functions and single lines early, and debugging with breakpoints and watchpoints to find and fix a wrong loop start.
A search that works for one item can still fail for another. Testing each line early prevents a small mistake from growing into a serious failure later.
In software development, a unit test checks that every module of code functions properly, so the code performs as desired. The aim is to test each line of code, because finding an error early prevents more serious issues later.
A unit test also outlines what each section of code does, which helps developers understand and maintain the code. When the tests keep passing after changes or rearrangement, the team gains confidence that the main functionality still works.
A unit test can be applied to a function, a module or even a single line of code. A formula for Zakat on yield from agricultural land is tested with different values for different possible cases until the line passes several unit tests. Such tests improve code quality by building confidence in making changes, catching errors early and making code management easier.
The example uses a shopping list containing apples, bread, milk and cheese, with a function called findItem that returns true when the item is present and false otherwise. The first test searches for milk, which exists, and expects true. The second test searches for eggs, which does not exist, and expects false. The outcomes are shown on the page based on the return value of findItem. The page layout stays simple, with a title and a paragraph that displays the results, and findItem itself is left unchanged for clarity.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Unit Tests for Array Search</title>
</head>
<body>
<h1>Unit Tests for `findItem` Function</h1>
<p id="testResults"></p>
<script>
const shoppingList = ["apples", "bread", "milk", "cheese"];
function findItem(itemName) {
for (let i = 0; i < shoppingList.length; i++) {
if (shoppingList[i] == itemName) {
return true;
}
The second half closes the search, then builds a result string and writes it into the page. It checks milk against true and eggs against false, and stores Passed or Failed for each case.
}
return false;
}
let testResults = "";
testResults += "Test 1 (Existing Item): ";
if (findItem("milk") == true) {
testResults += "Passed \n";
} else {
testResults += "Failed \n";
}
testResults += "\n Test 2 (Non-Existing Item): ";
if (findItem("eggs") == false) {
testResults += "Passed\n";
} else {
testResults += "Failed\n";
}
document.getElementById("testResults").textContent = testResults;
</script>
</body>
</html>
When the page runs, both checks pass, so the paragraph shows a passed message for the existing item and for the non-existing item.
A total that looks complete can still be wrong. Observing the code step by step shows flaws that affect layout or functionality, and correcting them at once builds the page more quickly.
Debugging identifies flaws that might affect the layout or functionality of a page. It lets the developer observe the operation of the code step by step, and building the site becomes quicker when mistakes are found and corrected at once. In tools such as Visual Studio.NET, this work centres on breakpoints and watchpoints.
A breakpoint is set where a line looks doubtful. When the program reaches that line, it pauses and lets the developer examine variable values and data flow.
In a watchpoint, the developer sets a rule on variables or expressions, for example whether the value is used or changed. The program pauses at that watchpoint when the rule is met. Both tools can point to issues that affect how the site works.
The example returns to adding the first 10 integers. A bug was placed in the loop on line 13 for discussion, of the kind that happens through typing errors.
As the loop started, the value of was shown as 10 in the variables pane, and the same starting problem appeared for in the watch pane. Because the task is adding the first 10 integers, the loop must start at the first integer. The fault was therefore a loop that began with instead of . After the correction the loop ran from the first integer, and the bug was identified and fixed.