Module 3: JavaScript Programming for Web Applications

Looking for ‘Introduction to HTML, CSS, & JavaScript Module 3 Answers’?

In this post, I provide complete, accurate, and detailed explanations for the answers to Module 3: JavaScript Programming for Web Applications of Course 5: Introduction to HTML, CSS, & JavaScript IBM AI Developer Professional Certificate .

Whether you’re preparing for quizzes or brushing up on your knowledge, these insights will help you master the concepts effectively. Let’s dive into the correct answers and detailed explanations for each question!

Practice Quiz

Practice Assignment

1. True or False: 10car is a valid variable name in JavaScript.

  • True
  • False ✅

Explanation:
Variable names cannot begin with a number. 10car is invalid.

2. Which of the following statement is the correct way to define a function with no parameters in JavaScript?

  • function = myFunction() { }
  • function = new function(myfunction)
  • function myFunction { }
  • function myFunction() { } ✅

Explanation:
This is the standard JavaScript syntax for defining a function.

3. Which of the following statements is the correct way to include a script in an HTML?

  • <script ref= “/source/script.js”>
  • </script> <include script = “/source/script.js”> </script>
  • <script name = “/source/script.js”> </script>
  • <script src = “/source/script.js”> </script> ✅

Explanation:
Use the src attribute to link an external JavaScript file.

4. How would you display a confirmation dialog box in a window?

  • window.alert(confirmation, “message”)
  • window.prompt(“message”)
  • window.alert(“message”)
  • window.confirm(“message”) ✅

Explanation:
window.confirm() shows a dialog box with OK and Cancel.

5. Select all the following statements about errors which are true.

  • To create a new custom “InputError”, the correct code would be: throw new Error(“InputError”, “The input provided is invalid”)
  • Error instance objects contain one property which contains information about the error
  • JavaScript has 6 core types of errors ✅
  • RangeError is created when a numeric value or parameter is outside a valid range ✅

Explanation:

  • JavaScript has 6 core types of errors
  • RangeError is created when a numeric value or parameter is outside a valid range

Graded Quiz

Graded Assignment

6. In the following declaration, what is the type of the variable ‘pi’?

  • string ✅
  • char
  • number
  • float

Explanation:
Even though it looks like a number, it’s enclosed in quotes, so it’s a string.

7. How do you define an array called array1 in JavaScript?

  • var array1 = new Array((1,2,3))
  • var array1 = new Array[1,2,3]
  • var array1 = [1,2,3] ✅
  • var array1 = (1,2,3)

Explanation:
This is the correct and most concise way to create an array.

8. What does the following statement do?

var ndate = new Date() ;

  • Assigns the current Greenwich Mean Time to ndate
  • Returns an error
  • Assigns the current local time to ndate ✅
  • Assigns an empty string with the properties of dates to ndate

Explanation:
new Date() gets the local date and time.

9. Which DOM function returns a node object matching a div with an id value “example_id”?

  • document.getElementById(div, “example_id”)
  • div.getValueOf(“example_id”)
  • element.getNodeById(“example_id”)
  • document.getElementById(“example_id”) ✅

Explanation:
This is the correct DOM method to fetch an element by id.

10. How are numbers converted to strings?

  • (123).toString() ✅
  • (123).string
  • string(123)
  • toString(123)

Explanation:
toString() is a method on number objects to convert to a string.

11. What is the value of ‘total’ after the following statement is executed?

var total = 10 + 1 +” 3”;

  • “113” ✅
  • “1013”
  • 14
  • This results in an error

Explanation:
JS evaluates left to right: 10 + 1 = 11, then 11 + " 3" = "113" (string concatenation).

12. What would the alert be, when the following code is executed?

var a = new String(“Hello”);

var b = “Hello”;

if (a ===b){

alert(“Same”);

}else{

alert(“Different”);

}

  • Same
  • Different ✅
  • It would not give any alert as it is an error
  • None of the above

Explanation:
a is an object (new String()), b is a primitive string. Strict equality === checks both type and value, so they’re not equal.

13. Which of the following is the proper way to create a `for` loop?

  • loop (for i = minVal; i < maxVal; i++) { … }
  • for (var i = minVal; i++; i < maxVal) { … }
  • for (var i = minVal; i < maxVal; i++) { … } ✅
  • for (i < maxVal) { … }

Explanation:
Correct for loop syntax is:
for (initialization; condition; increment)

14. Select all of the following which are properways to add a `color` property to a custom `Car` object.

  • Modify the code of the Car object directly to add a color parameter in the constructor ✅
  • Car.color = “Red”
  • Car.prototype.color = “Red” ✅
  • Car.prototype(Color, “Red”)

Explanation:

  • You can add a property through the constructor.
  • Or by adding it to the prototype for shared access.

15. Which of the following is not an event binder in JavaScript?

  • onclick
  • onload
  • onhover ✅
  • onmouseover

Explanation:
There’s no onhover event in JS. The correct one is onmouseover.

Leave a Reply