Data Types - Exercise 1
Question 1: Describe a Value's Type Completed
Return "null" for null, "array" for arrays, and otherwise the result of typeof.
Expected output: "array" (print this with console.log)
function describeType(value) {
// Write your solution here
}
console.log(describeType([1,2,3]));function describeType(value) {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
return typeof value;
}
console.log(describeType([1,2,3]));Question 2: Identify a Primitive Completed
Return true if the value is a primitive, including null; otherwise return false.
Expected output: false (print this with console.log)
function isPrimitiveValue(value) {
// Write your solution here
}
console.log(isPrimitiveValue({"id":4}));function isPrimitiveValue(value) {
return value === null || (typeof value !== "object" && typeof value !== "function");
}
console.log(isPrimitiveValue({"id":4}));Question 3: Double a Numeric Value Completed
Convert the supplied numeric string to a number and return twice its value.
Expected output: 25 (print this with console.log)
function doubleNumericText(text) {
// Write your solution here
}
console.log(doubleNumericText("12.5"));function doubleNumericText(text) {
return Number(text) * 2;
}
console.log(doubleNumericText("12.5"));Question 4: Label Missing Values Completed
Return "missing" for null or undefined and "present" for any other value.
Expected output: "missing" (print this with console.log)
function labelMissingValue(value) {
// Write your solution here
}
console.log(labelMissingValue(null));function labelMissingValue(value) {
return value == null ? "missing" : "present";
}
console.log(labelMissingValue(null));Question 5: Create a Name Tag Completed
Convert the supplied value to a string and return it prefixed with "Name: ".
Expected output: "Name: 2024" (print this with console.log)
function makeNameTag(value) {
// Write your solution here
}
console.log(makeNameTag(2024));function makeNameTag(value) {
return "Name: " + String(value);
}
console.log(makeNameTag(2024));Question 6: Check Positive Balance Completed
Return a boolean indicating whether the balance is greater than zero.
Expected output: false (print this with console.log)
function hasPositiveBalance(balance) {
// Write your solution here
}
console.log(hasPositiveBalance(0));function hasPositiveBalance(balance) {
return balance > 0;
}
console.log(hasPositiveBalance(0));Question 7: Invert a Flag Completed
Return the opposite of the supplied boolean.
Expected output: false (print this with console.log)
function invertFlag(enabled) {
// Write your solution here
}
console.log(invertFlag(true));function invertFlag(enabled) {
return !enabled;
}
console.log(invertFlag(true));Question 8: Use a Default Only for Undefined Completed
Return "guest" when the argument is undefined; return the argument unchanged otherwise.
Expected output: "guest" (print this with console.log)
function defaultUndefinedName(name) {
// Write your solution here
}
console.log(defaultUndefinedName());function defaultUndefinedName(name) {
return name === undefined ? "guest" : name;
}
console.log(defaultUndefinedName());Question 9: Replace Null Only Completed
Return "not set" if the argument is null; otherwise return the original value.
Expected output: "not set" (print this with console.log)
function replaceNullValue(value) {
// Write your solution here
}
console.log(replaceNullValue(null));function replaceNullValue(value) {
return value === null ? "not set" : value;
}
console.log(replaceNullValue(null));Question 10: List Value Types Completed
Return an array containing the typeof result for each supplied value.
Expected output: ["number","string","boolean"] (print this with console.log)
function listValueTypes(values) {
// Write your solution here
}
console.log(listValueTypes([42,"hi",false]));function listValueTypes(values) {
return values.map((value) => typeof value);
}
console.log(listValueTypes([42,"hi",false]));