Set - Exercise 1
Question 1: Create a Set from Values Completed
Create a Set from the supplied array and return its values as an array in insertion order.
Expected output: ["a","b"] (print this with console.log)
function setValuesInOrder(values) {
// Write your solution here
}
console.log(setValuesInOrder(["a","b","a"]));function setValuesInOrder(values) {
return [...new Set(values)];
}
console.log(setValuesInOrder(["a","b","a"]));Question 2: Count Unique Set Values Completed
Return the number of unique values in the input using a Set.
Expected output: 3 (print this with console.log)
function setUniqueValueCount(values) {
// Write your solution here
}
console.log(setUniqueValueCount([1,2,1,3]));function setUniqueValueCount(values) {
return new Set(values).size;
}
console.log(setUniqueValueCount([1,2,1,3]));Question 3: Check Set Membership Completed
Create a Set from the values and return whether it contains the supplied target.
Expected output: true (print this with console.log)
function setContainsTarget(values, target) {
// Write your solution here
}
console.log(setContainsTarget(["red","blue"], "blue"));function setContainsTarget(values, target) {
return new Set(values).has(target);
}
console.log(setContainsTarget(["red","blue"], "blue"));Question 4: Add a Value to a Set Completed
Create a Set from the input, add the supplied value, and return the values in insertion order.
Expected output: [1,2,3] (print this with console.log)
function addSetValue(values, value) {
// Write your solution here
}
console.log(addSetValue([1,2], 3));function addSetValue(values, value) {
const set = new Set(values);
set.add(value);
return [...set];
}
console.log(addSetValue([1,2], 3));Question 5: Remove a Value from a Set Completed
Create a Set from the input, delete the supplied value, and return its remaining values in insertion order.
Expected output: [1,3] (print this with console.log)
function removeSetValue(values, value) {
// Write your solution here
}
console.log(removeSetValue([1,2,3], 2));function removeSetValue(values, value) {
const set = new Set(values);
set.delete(value);
return [...set];
}
console.log(removeSetValue([1,2,3], 2));Question 6: Report a Set Deletion Completed
Create a Set from the input, delete the supplied value, and return whether an element was removed.
Expected output: true (print this with console.log)
function reportSetDeletion(values, value) {
// Write your solution here
}
console.log(reportSetDeletion(["a","b"], "a"));function reportSetDeletion(values, value) {
return new Set(values).delete(value);
}
console.log(reportSetDeletion(["a","b"], "a"));Question 7: Clear a Set Completed
Create a Set from the values, record its size, clear it, and return an array with its sizes before and after clearing.
Expected output: [2,0] (print this with console.log)
function setSizesBeforeAfterClear(values) {
// Write your solution here
}
console.log(setSizesBeforeAfterClear([1,2,2]));function setSizesBeforeAfterClear(values) {
const set = new Set(values);
const before = set.size;
set.clear();
return [before, set.size];
}
console.log(setSizesBeforeAfterClear([1,2,2]));Question 8: Add Several Set Values Completed
Create a Set from the first array, add each value from the second, and return the result in insertion order.
Expected output: [1,2,3,4] (print this with console.log)
function addMultipleSetValues(initial, added) {
// Write your solution here
}
console.log(addMultipleSetValues([1,2], [2,3,4]));function addMultipleSetValues(initial, added) {
const set = new Set(initial);
for (const value of added) set.add(value);
return [...set];
}
console.log(addMultipleSetValues([1,2], [2,3,4]));Question 9: Iterate over a Set Completed
Create a Set from the numeric input and return the sum of its unique values.
Expected output: 5 (print this with console.log)
function sumUniqueSetValues(numbers) {
// Write your solution here
}
console.log(sumUniqueSetValues([2,2,3]));function sumUniqueSetValues(numbers) {
let total = 0;
for (const value of new Set(numbers)) total += value;
return total;
}
console.log(sumUniqueSetValues([2,2,3]));Question 10: Return Set Values in Reverse Completed
Create a Set and return its distinct values in reverse insertion order.
Expected output: ["c","b","a"] (print this with console.log)
function reverseSetInsertionOrder(values) {
// Write your solution here
}
console.log(reverseSetInsertionOrder(["a","b","a","c"]));function reverseSetInsertionOrder(values) {
return [...new Set(values)].reverse();
}
console.log(reverseSetInsertionOrder(["a","b","a","c"]));