JavaScript Question 7 (1 point) Code Example 5-1 1. var getResult = function() {
ID: 3914101 • Letter: J
Question
JavaScript
Question 7 (1 point)
Code Example 5-1
1. var getResult = function() {
2. var num1 = 54.95;
3. var num2 = .1;
4. var result = parseFloat(prompt("What is " + num1 + " * " + num2 + "?"));
5. if (result != (num1 * num2)) {
6. alert("Incorrect");
7. }
8. else {
9. alert("Correct!");
10. }
11. };
(Refer to Code Example 5-1) Which of the following would fix the error in the code example?
Question 7 options:
Change the prompt on line 4 to read:
"What is " + num1 + " * " + num2 + ", rounded to 3 decimal places?"
change the condition of the if statement on line 5 to:
if (result != (num1 * num2).toFixed(3))
change the if statement (lines 5 - 10) to read:
if (result == (num1 * num2)) {
alert("Correct!");
}
else {
alert("Incorrect");
}
change the condition of the if statement on line 5 to:
(result != parseFloat(num1 * num2)
Save
Question 8 (1 point)
To avoid potential errors caused by using variables that are not properly declared, you should
Question 8 options:
make sure you always assign values to your variables
use strict mode
make all variables global
make all variables local
Save
Question 9 (1 point)
The code that follows has a bug in it because the second use of the variable named salesTax is spelled with all lowercase letters (salestax).
var calculateTax = function(subtotal,taxRate) {
var salesTax = subtotal * taxRate;
salestax = parseFloat(salesTax.toFixed(2));
return salesTax;
};
Assuming that there are no other problems and that you’re using strict mode, what will happen when this code is executed?
Question 9 options:
The salesTax variable will contain the right value, but it won’t be rounded.
An error will occur because salestax hasn’t been declared.
The salestax variable will contain the rounded sales tax value.
The salesTax variable will contain null when it is returned.
Save
Question 10 (1 point)
What will be displayed in the user's browser after the following code executes if the user enters 87 at the prompt?
"use strict"
var getInfo = function() {
grade = parseInt(prompt("What's your score on the test?"));
var newGrade = getResult(grade);
alert("Your grade, curved, is " + newGrade);
};
var getResult = function(grade) {
var newGrade = grade + 5;
return (newGrade);
};
Question 10 options:
an alert will display: Your grade, curved, is 92
an alert will display: Your grade, curved, is NaN
nothing will display; grade is an undeclared variable
nothing will display; newGrade cannot be declared twice
Save
Question 11 (1 point)
Code Example 5-2
var getAvg = function () {
var count = 1; var itemTotal = 0; var item = 0;
while (count < 4) {
item = parseInt(prompt("Enter item cost: "));
itemTotal += item;
count++;
}
var averageCost = parseInt(itemTotal/count);
alert("Total cost: $" + itemTotal + ", Average cost: $" + averageCost);
};
(Refer to Code Example 5-2) Suppose you tested the getAvg function by entering the values 5, 10, and 15 when prompted and the alert() method displayed “Total cost: $30, Average cost: $7”. What type of error is this?
Question 11 options:
runtime error
logic error
syntax error
JavaScript engine error
Save
Question 12 (1 point)
Code Example 5-2
var getAvg = function () {
var count = 1; var itemTotal = 0; var item = 0;
while (count < 4) {
item = parseInt(prompt("Enter item cost: "));
itemTotal += item;
count++;
}
var averageCost = parseInt(itemTotal/count);
alert("Total cost: $" + itemTotal + ", Average cost: $" + averageCost);
};
(Refer to Code Example 5-2) How would you fix the error in the calculation of the average cost in this code?
Question 12 options:
by changing the condition in the while loop to count < 3
by changing the initial value of count to 0
by changing the value of averageCost to parseInt(itemTotal/(count - 1))
by using commas, not semicolons, between variable declarations
Save
Question 13 (1 point)
Which of the following types of errors will cause an application to stop executing?
Question 13 options:
runtime error
logic error
syntax error
JavaScript engine error
Save
Change the prompt on line 4 to read:
"What is " + num1 + " * " + num2 + ", rounded to 3 decimal places?"
change the condition of the if statement on line 5 to:
if (result != (num1 * num2).toFixed(3))
change the if statement (lines 5 - 10) to read:
if (result == (num1 * num2)) {
alert("Correct!");
}
else {
alert("Incorrect");
}
change the condition of the if statement on line 5 to:
(result != parseFloat(num1 * num2)
Explanation / Answer
Q7
The correct option will be
change the condition of the if statement on line 5 to:
(result != parseFloat(num1 * num2)
Q8
make sure you always assign values to your variables
Q9
The salestax variable will contain the rounded sales tax value.
variable can be assigned without the use of var keyword also
Q10
an alert will display: Your grade, curved, is 92
Q11
logic error
Q12
by changing the value of averageCost to parseInt(itemTotal/(count - 1))
Q13
Syntax error
java script engine error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.