You are testing the value of the following variable in JavaScript.
var height = -300-;
A block of code must execute if the following conditions are true:
The height variable is set to 300
The height variable is of type string
You need to implement the code to run the test.
Which line of code should you use?
A. if (height = = 300)
B. if (height = = -300-)
C. if (height ! -300-)
D. if (height ! = 300)
Explanation:
Use = = to test for equality.
Use -300- to test for the string.
I think A is also the right answer.
0
1
I think that none of these answers are correct. Neither A nor B provides check for type string which is part of the requirements. They test only the value of 300/”300″. In my opinion correct answer should be:
if (height === “300”)
You can execute the following code yourself to test it:
$(function () {
wrongTestOfValue(300);
correctTestOfValue(300);
});
function wrongTestOfValue(value) {
if (value == “300”) {
alert(“Wrong test: Value is type of string and value of \”300\””);
}
else {
alert(“Wrong test: Value is either not type of string or not value of \”300\””);
}
}
function correctTestOfValue(value) {
if (value === “300”) {
alert(“Correct test: Value is type of string and value of \”300\””);
}
else {
alert(“Correct test: Value is either not type of string or not value of \”300\””);
}
}
3
0