Quiz and Test Module 3-SOLVED
Quiz and Test Module 3-SOLVED
Índice
JAVASCRIPT ESSENTIALS 1 (JSE) - MODULE 3 .............................................................................................................................3
JAVASCRIPT ESSENTIALS 1 (JSE) MODULE 3 ........................................................................................................................................................... 3
Operators and User Interaction ............................................................................................................................................................................. 3
QUIZ ....................................................................................................................................................................................................................4
TEST ................................................................................................................................................................................................................ 10
• know what operators are and how to classify them (by type of operand, by number of operands, etc.);
• be able to use assignment, arithmetic, logical, and comparison operators in practice;
• understand the operation of the conditional operator and the typeof, instanceof, and delete operators;
• understand what the precedence and associativity of basic operators are and be able to influence them
by means of bracket grouping;
• be able to perform basic two-way communication with the program user using the alert, confirm, and
prompt dialog boxes.
1.- The snippet starts with let n = which means we are declaring a variable n.
2.- Next, we have 5 + 2 ** 2 * 3.
d.- 17
The number 3 is stored in the variable x: (let x =3;). The command x=x*x wasthen called. This last
command can be replaced using:
a. x**=2;
b. x**=x;
c. x+=x;
d. x*= 2;
To replace the last command x = x * x using a shorthand assignment operator, you need to find
the correct shorthand operator that achieves the same result.
Given that x = 3, the command x = x * x will square the value of x.
a.- x **= 2;
In JavaScript, when you use the unary negation operator - on a string that represents a number, it tries
to convert the string to a number and then negates it. If the conversion is successful, it negates the
numeric value. If the string cannot be converted to a number, it results in NaN (Not-a-Number).
a.- -1024
c.- n: 11, m: 10
d.- 1
The expression 20 && 5 uses the logical AND operator &&. In JavaScript, the logical AND
operator returns the value of the second operand if both operands are truthy. If the first operand
is falsy, it immediately returns that value without evaluating the second operand.
Truthy values in JavaScript include non-zero numbers (like 20 and 5) and non-empty strings,
among others. Falsy values include 0, null, undefined, false, and empty strings ("").
c.- 5
In the expression true && "false", the logical AND operator && is used.
Truthy values include true, non-empty strings, and non-zero numbers, among others. Falsy values
include false, null, undefined, 0, and empty strings ("").
Now, let's evaluate the expression:
1.- true is a truthy value.
2.- "false" is a non-empty string, so it is also a truthy value.
Since both operands are truthy, the logical AND operator returns the value of the second operand,
which is "false".
d.- "false"
The operator we use to check if two variables store different values is:
The !== operator is the strict inequality operator in JavaScript, which checks if two values are not
equal, both in value and in type. It returns true if the values are different, and false if they are
equal.
To compare two strings in JavaScript, the comparison is performed character by character based
on their Unicode code points. The comparison stops when the first differing characters are found.
c.- false
The less-than operator < checks if the value on the left is less than the value on the right. In this
case, 12 is not less than 5, so the expression 12 < 5 evaluates to false.
c.- false
The method that will display a dialog box that allows the user to type in any string is:
c.- prompt
The prompt() method displays a dialog box with an optional message and an input field where
the user can type in a string. The user can then enter a string and click OK or Cancel. If the user
clicks OK, the entered string is returned as the result of the prompt() method. If the user clicks
Cancel or closes the dialog, the method returns null.
The confirm method allows you to create a dialog box. What value does this method return when the
user closes the window?
a. It depends on the option selected by the user.
b. It always returns false .
c. The string entered by the user.
d. It always returns true.
The confirm() method displays a dialog box with a message and two buttons: "OK" and
"Cancel". When the user interacts with the dialog box, the method returns a boolean value based
on the user's choice:
• If the user clicks "OK", the method returns true.
• If the user clicks "Cancel" or closes the dialog, the method returns false.
The code you provided uses the confirm() method to display a dialog box with the message "Are
you ready?" and two buttons: "OK" and "Cancel".
If the user presses the "OK" button in the dialog box, the confirm() method returns true.
Otherwise, if the user clicks "Cancel" or closes the dialog, the method returns false.
After running the code and pressing the "OK" button in the dialog box, the value of the choice
variable will be:
a.- true
The ternary operator ? : is used to assign the value based on the result of the confirm() method.
If the confirm() method returns true, the value of choice will be "true". Since the confirm()
method returns true when the "OK" button is pressed, the value of choice will be true.
To replace the last command n = n * n * n using a shorthand assignment operator, you need to
find the correct shorthand operator that achieves the same result.
Given that n = 2, the last command n = n * n * n will perform the operation 2 * 2 * 2, which
equals 8.
b.- n **= 3;
If, after running the code, we immediately press the OK button on the newly created dialog, the
value of the test variable will be:
c.- "World"
The prompt() method displays a dialog box with two buttons: "OK" and "Cancel", along with a
text input field where the user can enter a value. The text input field will contain the default value
"World" as specified in the second argument of the prompt() method.
If the user presses the OK button without entering any value or just clicks OK without making any
changes, the prompt() method will return the default value, which is "World" in this case.
Therefore, the ‘test’ variable will have the value "World".
When the user closes the window of the confirm dialog box (by clicking the "Cancel" button or
closing the dialog), the confirm() method returns:
The confirm() method returns a boolean value based on the user's choice. If the user clicks the
"OK" button in the dialog, the method returns true. But if the user clicks the "Cancel" button or
closes the dialog, the method returns false.
d.- 20
The greater-than operator > checks if the value on the left is greater than the value on the right.
In this case, 12 is greater than 5, so the expression 12 > 5 evaluates to true.
c.- true
d.- 53
c.- false
d.- 12
After executing the code snippet, the values of the variables n and m will be:
a. n: 11, m: 11
a.- n: 11, m: 11
In JavaScript, when comparing strings using the greater-than operator (>), the comparison is
based on the Unicode code point value of each character in the strings. The comparison is done
character by character from left to right until the first differing characters are found.
Now, let's compare the strings "abcd" and "Abcd" character by character:
1.- The first characters are compared: 'a' (Unicode code point 97) and 'A' (Unicode code point
65). The Unicode code point of 'a' (97) is greater than the Unicode code point of 'A' (65), so
"abcd" is considered to be greater than "Abcd".
Since the comparison has determined the result, the evaluation stops, and the overall result is
determined.
c.- true
Which operator do we use if we want to check if two variables store the same values of exactly the same
type?
a. ===
b. =
c. !==
d. ==
The operator we use to check if two variables store the same values of exactly the same type is:
The === operator is the strict equality operator in JavaScript, which checks if two values are
equal, both in value and in type. It returns true if the values are the same and of the same type,
and false if they are not.
a.- The methods window.alert, window.confirm, and window.prompt are methods of the window object.
Explanation:
a.- This statement is true. The window.alert, window.confirm, and window.prompt methods
are indeed methods of the window object in JavaScript.
b.- This statement is true. The window object represents the window in which the HTML
document containing the JavaScript code currently being executed is open. It serves as the
global object in the browser environment.
c.- This statement is true. The alert, confirm, and prompt methods display modal windows that
block access to the page until they are closed by the user.
d.- This statement is not true. In the global scope of a browser environment, you can call window
methods, such as window.alert, without explicitly including the name "window". For example,
calling alert("abc") would be correct, and it would display the same alert as
window.alert("abc"). However, if you are working in a local scope (e.g., inside a function), you
need to include the window object explicitly to avoid referencing a local variable with the same
name.
b. "false"
The logical OR operator || in JavaScript returns the first truthy value it encounters, or the last
value if all operands are falsy. In this case, the first operand is false, which is falsy. Since the
second operand is a non-empty string "false", which is a truthy value, the logical OR operator
returns "false".