Data Types - Exercise 1

Question 1: Describe a Value's Type

Return "null" for null, "array" for arrays, and otherwise the result of typeof.

Expected output: "array" (print this with console.log)

javascript

function describeType(value) {
  // Write your solution here
}

console.log(describeType([1,2,3]));

Question 2: Identify a Primitive

Return true if the value is a primitive, including null; otherwise return false.

Expected output: false (print this with console.log)

javascript

function isPrimitiveValue(value) {
  // Write your solution here
}

console.log(isPrimitiveValue({"id":4}));

Question 3: Double a Numeric Value

Convert the supplied numeric string to a number and return twice its value.

Expected output: 25 (print this with console.log)

javascript

function doubleNumericText(text) {
  // Write your solution here
}

console.log(doubleNumericText("12.5"));

Question 4: Label Missing Values

Return "missing" for null or undefined and "present" for any other value.

Expected output: "missing" (print this with console.log)

javascript

function labelMissingValue(value) {
  // Write your solution here
}

console.log(labelMissingValue(null));

Question 5: Create a Name Tag

Convert the supplied value to a string and return it prefixed with "Name: ".

Expected output: "Name: 2024" (print this with console.log)

javascript

function makeNameTag(value) {
  // Write your solution here
}

console.log(makeNameTag(2024));

Question 6: Check Positive Balance

Return a boolean indicating whether the balance is greater than zero.

Expected output: false (print this with console.log)

javascript

function hasPositiveBalance(balance) {
  // Write your solution here
}

console.log(hasPositiveBalance(0));

Question 7: Invert a Flag

Return the opposite of the supplied boolean.

Expected output: false (print this with console.log)

javascript

function invertFlag(enabled) {
  // Write your solution here
}

console.log(invertFlag(true));

Question 8: Use a Default Only for Undefined

Return "guest" when the argument is undefined; return the argument unchanged otherwise.

Expected output: "guest" (print this with console.log)

javascript

function defaultUndefinedName(name) {
  // Write your solution here
}

console.log(defaultUndefinedName());

Question 9: Replace Null Only

Return "not set" if the argument is null; otherwise return the original value.

Expected output: "not set" (print this with console.log)

javascript

function replaceNullValue(value) {
  // Write your solution here
}

console.log(replaceNullValue(null));

Question 10: List Value Types

Return an array containing the typeof result for each supplied value.

Expected output: ["number","string","boolean"] (print this with console.log)

javascript

function listValueTypes(values) {
  // Write your solution here
}

console.log(listValueTypes([42,"hi",false]));