Data Types - Exercise 4
Question 1: Count Falsy Entries Completed
Return the number of falsy values in the supplied array.
Expected output: 3 (print this with console.log)
function countFalsyEntries(values) {
// Write your solution here
}
console.log(countFalsyEntries([0,"","0",null,true]));function countFalsyEntries(values) {
return values.filter((value) => !value).length;
}
console.log(countFalsyEntries([0,"","0",null,true]));Question 2: Keep Truthy Entries Completed
Return a new array containing only the truthy values, in their original order.
Expected output: ["yes",9] (print this with console.log)
function keepTruthyEntries(values) {
// Write your solution here
}
console.log(keepTruthyEntries([0,"yes",false,9]));function keepTruthyEntries(values) {
return values.filter(Boolean);
}
console.log(keepTruthyEntries([0,"yes",false,9]));Question 3: Format an Invoice Number Completed
Convert the supplied number to a string prefixed with "Invoice #".
Expected output: "Invoice #57" (print this with console.log)
function formatInvoiceNumber(number) {
// Write your solution here
}
console.log(formatInvoiceNumber(57));function formatInvoiceNumber(number) {
return "Invoice #" + String(number);
}
console.log(formatInvoiceNumber(57));Question 4: Format a Boolean Setting Completed
Convert the supplied boolean to a string prefixed with "enabled: ".
Expected output: "enabled: false" (print this with console.log)
function formatBooleanSetting(enabled) {
// Write your solution here
}
console.log(formatBooleanSetting(false));function formatBooleanSetting(enabled) {
return "enabled: " + String(enabled);
}
console.log(formatBooleanSetting(false));Question 5: Convert Empty Text Completed
Convert the supplied string with Number and return the resulting number.
Expected output: 0 (print this with console.log)
function numberFromEmptyText(text) {
// Write your solution here
}
console.log(numberFromEmptyText(""));function numberFromEmptyText(text) {
return Number(text);
}
console.log(numberFromEmptyText(""));Question 6: Convert an Array to Text Completed
Use String to convert the supplied array and return the resulting text.
Expected output: "1,2,3" (print this with console.log)
function arrayAsText(values) {
// Write your solution here
}
console.log(arrayAsText([1,2,3]));function arrayAsText(values) {
return String(values);
}
console.log(arrayAsText([1,2,3]));Question 7: Convert Text to Boolean Completed
Return the Boolean conversion of the supplied text.
Expected output: true (print this with console.log)
function textAsBoolean(text) {
// Write your solution here
}
console.log(textAsBoolean("false"));function textAsBoolean(text) {
return Boolean(text);
}
console.log(textAsBoolean("false"));Question 8: Check Array Values Completed
Return true if the supplied value is an array and false otherwise.
Expected output: false (print this with console.log)
function checkArrayValue(value) {
// Write your solution here
}
console.log(checkArrayValue({"length":2}));function checkArrayValue(value) {
return Array.isArray(value);
}
console.log(checkArrayValue({"length":2}));Question 9: Distinguish Collections Completed
Return "array" for arrays, "object" for other non-null objects, and "other" otherwise.
Expected output: "object" (print this with console.log)
function collectionKind(value) {
// Write your solution here
}
console.log(collectionKind({"item":1}));function collectionKind(value) {
if (Array.isArray(value)) return "array";
return value !== null && typeof value === "object" ? "object" : "other";
}
console.log(collectionKind({"item":1}));Question 10: Copy a Record Completed
Return a shallow copy of the supplied object with an additional saved property set to true.
Expected output: {"id":8,"saved":true} (print this with console.log)
function copySavedRecord(record) {
// Write your solution here
}
console.log(copySavedRecord({"id":8}));function copySavedRecord(record) {
return { ...record, saved: true };
}
console.log(copySavedRecord({"id":8}));