A class was written to represent items for purchase in an online store, and a second class representing items that are on sale at a discounted price. The constructor sets the name to the first value passed in. There is a new requirement for a developer to implement a description method that will return a brief description for Item and SaleItem.
01 let regItem = new Item('Scarf', 55);
02 let saleItem = new SaleItem('Shirt', 80, .1);
03 Item.prototype.description = function() { return 'This is a ' + this.name; }
04 console.log(regItem.description());
05 console.log(saleItem.description());
06
07 SaleItem.prototype.description = function() { return 'This is a discounted ' + this.name; }
What is the output when executing the code above?
Comprehensive and Detailed
Assuming SaleItem inherits from Item via prototype (e.g. SaleItem.prototype = Object.create(Item.prototype)):
Lines 01--02: create regItem and saleItem.
Line 03: define Item.prototype.description.
Now both regItem and saleItem (via inheritance) have a description method from Item.prototype.
Line 04: regItem.description() 'This is a Scarf'.
Line 05: saleItem.description() 'This is a Shirt' (same method, but this.name is 'Shirt').
Line 07: SaleItem.prototype.description = ... overrides description only for SaleItem instances going forward.
If we imagine calling regItem.description() and saleItem.description() again after line 07:
regItem.description() still uses Item.prototype.description 'This is a Scarf'.
saleItem.description() now uses SaleItem.prototype.description 'This is a discounted Shirt'.
Those four lines correspond to option B.
________________________________________
Given the code below:
let numValue = 1982;
Which three code segments result in a correct conversion from number to string?
Comprehensive and Detailed Explanation From JavaScript Knowledge:
We want to convert the number 1982 to a string.
Check each option:
A . numValue.toText()
There is no standard toText() method on numbers.
This will result in TypeError: numValue.toText is not a function.
B . String(numValue);
String() as a function converts its argument to a string.
String(1982) returns '1982'.
This is correct.
C . '' + numValue;
'' is a string; + with a string operand performs string concatenation.
'' + 1982 '1982'.
This is a common shorthand for number-to-string conversion.
D . numValue.toString();
Number.prototype.toString() converts the number to its string representation.
1982..toString() or (1982).toString() returns '1982'.
For the variable, numValue.toString() is valid: '1982'.
E . (String)numValue;
This is not valid JavaScript casting syntax; it is more like a C/Java-style cast.
In JavaScript, that is parsed as a grouping expression (String) and then numValue; it does not convert numValue to a string.
Thus the correct answers are:
B . String(numValue);
C . '' + numValue;
D . numValue.toString();
Relevant concepts: primitive type conversion, String() casting, .toString(), coercion via + with strings.
________________________________________
Refer to the code:
const pi = 3.1415926;
What is the data type of pi?
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
JavaScript has one numeric data type for all real numbers, whether integers or decimals.
This type is simply called:
Number
It follows the IEEE 754 double-precision floating-point standard internally,
but JavaScript does not expose separate types like float, double, or decimal.
Therefore:
It is not Float JavaScript does not have float primitives.
It is not Double this refers to the underlying IEEE 754 representation, but JavaScript's type is still just ''Number.''
It is not Decimal JavaScript has no built-in decimal type.
The correct answer is Number.
________________________________________
JavaScript Knowledge Reference (text-only)
JavaScript has a single numeric type: Number.
All numbers---integers, fractions, floating point---use the Number type.
==================================================
Which actions can be done using the JavaScript browser console?
A: Yes. You can run arbitrary JS in the console (even if it doesn't touch the page).
B: Yes. You can inspect and modify DOM nodes via JS.
C: Performance reports are mainly in the Performance tab, not the console.
D: Yes. You can alter the DOM and redefine or override JS functions/variables at runtime.
E: You can read and set document.cookie, but HttpOnly ''security cookies'' cannot be read or changed from JS.
________________________________________
Phyliss
4 days agoLeandro
12 days agoHelga
19 days agoBrent
26 days ago