Question: For this discussion, we will be looking at a simple HTML file (index.htm) and an external JavaScript file (disc_3.js). Open both files in your text

For this discussion, we will be looking at a simple HTML file (index.htm) and an external JavaScript file (disc_3.js). Open both files in your text editor and examine their contents; also open index.htm in a web browser. The HTML file and JavaScript file work together to allow the user to enter their favorite car makes into the textbox. The addCar() function in disc_3.js then adds the car makes to an array. Once the array reaches four entries, the array contents are output to a

element with an ID of "output".

In its current form, disc_3.js contains three errors having to do with arrays, loops, and if statements. For this discussion, please do the following: 1) Identify all three errors 2) Explain why the errors prevent the code form working 3) Provide solutions to each error

Please upload your corrected JavaScript file as LASTNAME_FIRSTNAME_disc_3.js

Content of index.htm:

Discussion 3

Discussion 3

Favorite Cars

Enter your four favorite cars into the textbox.






Content of disc_3.js:

// This program uses an array to store a list of favorite car makes and then // outputs the contents to an HTML page.

var cars; // Array to hold car makes (Error 1: not an array) var i = 0; // Index

// The addCar() function will add a car from the textbox to the cars[] array. // The array is output once it contains 4 entries. function addCar() { if (i < 4) { cars[i] = input.value; input.value = ""; i++; if (i = 4) { // Error 2: assignment instead of comparison displayCars(); } } }

// The displayCars() function uses a for loop to loop through the cars[] array // and output its contents to the

element with ID "output" function displayCars() { // Reference the element with ID "output" for simpler code var output = document.getElementById("output");

output.innerHTML += "Your list is: "; for (var j = 1; j < cars.length; j++) { // Error 3: j should start at 0 output.innerHTML += cars[j]; if (j < cars.length - 1) output.innerHTML += ", "; } }

// Backward compatible event listener if (document.getElementById("submit").addEventListener) { document.getElementById("submit").addEventListener("click", addCar, false); } else if (document.getElementById("submit").attachEvent) { document.getElementById("submit").attachEvent("onclick", addCar); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!