Data Types - Exercise 2
Question 1: Create a Symbol Label Completed
Create a Symbol using the supplied description and return its description.
Expected output: "ticket" (print this with console.log)
function symbolLabel(label) {
// Write your solution here
}
console.log(symbolLabel("ticket"));function symbolLabel(label) {
return Symbol(label).description;
}
console.log(symbolLabel("ticket"));Question 2: Compare New Symbols Completed
Create two Symbols using the supplied description and return an object containing their description and whether they are strictly equal.
Expected output: {"description":"id","equal":false} (print this with console.log)
function sameNewSymbols(label) {
// Write your solution here
}
console.log(sameNewSymbols("id"));function sameNewSymbols(label) {
const first = Symbol(label);
const second = Symbol(label);
return { description: first.description, equal: first === second };
}
console.log(sameNewSymbols("id"));Question 3: Read an Object Field Completed
Return the city property of the supplied object.
Expected output: "Kyoto" (print this with console.log)
function readCity(place) {
// Write your solution here
}
console.log(readCity({"city":"Kyoto"}));function readCity(place) {
return place.city;
}
console.log(readCity({"city":"Kyoto"}));Question 4: Create a Product Record Completed
Return an object with name and price properties using the supplied arguments.
Expected output: {"name":"Pen","price":3} (print this with console.log)
function makeProductRecord(name, price) {
// Write your solution here
}
console.log(makeProductRecord("Pen", 3));function makeProductRecord(name, price) {
return { name, price };
}
console.log(makeProductRecord("Pen", 3));Question 5: Check for a Callable Value Completed
When enabled is true, create a function; otherwise use the supplied text. Return whether the resulting value has typeof "function".
Expected output: true (print this with console.log)
function isCallableValue(enabled, text) {
// Write your solution here
}
console.log(isCallableValue(true, "hello"));function isCallableValue(enabled, text) {
const value = enabled ? () => text : text;
return typeof value === "function";
}
console.log(isCallableValue(true, "hello"));Question 6: Add a Numeric String Completed
Convert the second argument to a number and return the sum.
Expected output: 15 (print this with console.log)
function addNumericText(amount, text) {
// Write your solution here
}
console.log(addNumericText(7, "8"));function addNumericText(amount, text) {
return amount + Number(text);
}
console.log(addNumericText(7, "8"));Question 7: Join as Text Completed
Return both supplied values joined as strings, without a separator.
Expected output: "56" (print this with console.log)
function joinAsText(left, right) {
// Write your solution here
}
console.log(joinAsText(5, 6));function joinAsText(left, right) {
return String(left) + String(right);
}
console.log(joinAsText(5, 6));Question 8: Convert a Flag to Number Completed
Explicitly convert the supplied boolean to a number and return it.
Expected output: 1 (print this with console.log)
function flagAsNumber(flag) {
// Write your solution here
}
console.log(flagAsNumber(true));function flagAsNumber(flag) {
return Number(flag);
}
console.log(flagAsNumber(true));Question 9: Compare Without Coercion Completed
Return whether the supplied values are strictly equal.
Expected output: false (print this with console.log)
function strictlyMatches(left, right) {
// Write your solution here
}
console.log(strictlyMatches(5, "5"));function strictlyMatches(left, right) {
return left === right;
}
console.log(strictlyMatches(5, "5"));Question 10: Parse a Decimal Price Completed
Convert the supplied decimal string to a number and return it.
Expected output: 19.75 (print this with console.log)
function parseDecimalPrice(text) {
// Write your solution here
}
console.log(parseDecimalPrice("19.75"));function parseDecimalPrice(text) {
return Number(text);
}
console.log(parseDecimalPrice("19.75"));