Data Types - Exercise 3
Question 1: Add Large Integers Completed
Convert both integer strings to BigInt, add them, and return the result as a string.
Expected output: "9007199254741000" (print this with console.log)
function addLargeIntegers(left, right) {
// Write your solution here
}
console.log(addLargeIntegers("9007199254740993", "7"));function addLargeIntegers(left, right) {
return (BigInt(left) + BigInt(right)).toString();
}
console.log(addLargeIntegers("9007199254740993", "7"));Question 2: Double a Big Integer Completed
Convert the integer string to BigInt, multiply by 2n, and return the result as a string.
Expected output: "24691357802469135780" (print this with console.log)
function doubleBigInteger(text) {
// Write your solution here
}
console.log(doubleBigInteger("12345678901234567890"));function doubleBigInteger(text) {
return (BigInt(text) * 2n).toString();
}
console.log(doubleBigInteger("12345678901234567890"));Question 3: Count Array Entries Completed
Return the number of items in the supplied array.
Expected output: 3 (print this with console.log)
function countArrayEntries(items) {
// Write your solution here
}
console.log(countArrayEntries(["red","blue","green"]));function countArrayEntries(items) {
return items.length;
}
console.log(countArrayEntries(["red","blue","green"]));Question 4: Build an Array Pair Completed
Return an array containing the two supplied values in their original order.
Expected output: ["north","south"] (print this with console.log)
function buildArrayPair(first, second) {
// Write your solution here
}
console.log(buildArrayPair("north", "south"));function buildArrayPair(first, second) {
return [first, second];
}
console.log(buildArrayPair("north", "south"));Question 5: Classify Object Values Completed
Return "reference" for non-null objects and functions; return "primitive" for all other values.
Expected output: "reference" (print this with console.log)
function classifyReference(value) {
// Write your solution here
}
console.log(classifyReference([1]));function classifyReference(value) {
return value !== null && (typeof value === "object" || typeof value === "function") ? "reference" : "primitive";
}
console.log(classifyReference([1]));Question 6: Compare Null's typeof Completed
Return an array containing typeof null followed by typeof the supplied value.
Expected output: ["object","number"] (print this with console.log)
function typeOfNullValue(value) {
// Write your solution here
}
console.log(typeOfNullValue(3));function typeOfNullValue(value) {
return [typeof null, typeof value];
}
console.log(typeOfNullValue(3));Question 7: Detect an Invalid Conversion Completed
Convert the supplied text to a number and return whether the result is NaN.
Expected output: true (print this with console.log)
function invalidNumberText(text) {
// Write your solution here
}
console.log(invalidNumberText("pear"));function invalidNumberText(text) {
return Number.isNaN(Number(text));
}
console.log(invalidNumberText("pear"));Question 8: Avoid NaN Coercion Completed
Return an array with Number.isNaN applied to the supplied value, followed by Number.isNaN applied after converting it with Number.
Expected output: [false,true] (print this with console.log)
function isActualNaN(value) {
// Write your solution here
}
console.log(isActualNaN("pear"));function isActualNaN(value) {
return [Number.isNaN(value), Number.isNaN(Number(value))];
}
console.log(isActualNaN("pear"));Question 9: Check a Finite Number Completed
Return true only if the supplied value is a finite number, without converting it.
Expected output: false (print this with console.log)
function isFiniteValue(value) {
// Write your solution here
}
console.log(isFiniteValue("10"));function isFiniteValue(value) {
return Number.isFinite(value);
}
console.log(isFiniteValue("10"));Question 10: Detect an Array Instance Completed
Return whether the supplied value is an instance of Array.
Expected output: true (print this with console.log)
function isArrayInstance(value) {
// Write your solution here
}
console.log(isArrayInstance([2,4]));function isArrayInstance(value) {
return value instanceof Array;
}
console.log(isArrayInstance([2,4]));