≡ Menu

JavaScript Assignment #2 – Creating a Set

JavaScript Assignment 2 Creating SetsIn today’s assignment we are going to tackle an issue that exists within JavaScript.

That issue is the fact that JavaScript has no Set data type. Gasp!

In Java, we use Sets like a type of array, only it doesn’t allow duplicates to be added.

So if you were using a Set and tried to insert the following elements:

1, 2, 3, 3, 4, 5

The resulting Set would only contain 5 elements even though you tried to add 6 elements.

The reason for this is that it would detect that you tried to add the number 3 twice. So the first time you insert 3, it accepts it and inserts it into the Set, but the second time you try to insert the number 3, it will NOT insert that element.

Everything else about a Set more or less acts like an array, so that’s the only thing we’ll concern ourselves with for this assignment.

What I want you to create is a method that will mimic the behavior of a Set in Java when you try to insert an element.

Here’s a basic outline of how it should work:

/** This function will only add numeric elements onto the end of the array
**   if the element being passed in doesn't already exist within the array
**   If the element is added successfully, then the function returns true, else it returns false
**/
function addNumericElementNoDuplicates (element, array) 
{
 // Step 1: Validate the element being passed in is of type number
 // Step 2: If it's NOT a number, return false, else continue
 // Step 3: traverse the array to see if the element to add already exists
 // Step 4: If the element already exists, return false
 // Step 5: If the element doesn't exist, then add it to the end of the array and return true.
}

Once you have completed your method, try creating the following test cases to ensure your code is functioning correctly.

Test Case 1

var myArray = [];
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(2, myArray);
addNumericElementNoDuplicates(3, myArray);
addNumericElementNoDuplicates(3, myArray);
addNumericElementNoDuplicates(4, myArray);
addNumericElementNoDuplicates(5, myArray);
console.log(myArray);

This test case should output the following to your console:

[1, 2, 3, 4, 5]

If it outputs anything else, then you likely have a bug in your code.

Test Case 2

var myArray = [];
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(2, myArray);
addNumericElementNoDuplicates(3, myArray);
addNumericElementNoDuplicates("test", myArray);
addNumericElementNoDuplicates(4, myArray);
addNumericElementNoDuplicates(5, myArray);
console.log(myArray);

This test case should output the following to your console:

[1, 2, 3, 4, 5]

If it outputs anything else, then you likely have a bug in your code.

Test Case 3

var myArray = [];
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(1, myArray);
addNumericElementNoDuplicates(1, myArray);
console.log(myArray);

This test case should output the following to your console:

[1]

If it outputs anything else, then you likely have a bug in your code.

In Summary

If you managed to implement the addNumericElementNoDuplicates method such that you get the expected results in all three test cases, then you have done a great job.
You can consider this assignment complete!

But, don’t throw away you code, because we’re going to use it as part of our next assignment. So keep that code in a safe place.

And if you really enjoyed this assignment (and haven’t done so already), be sure to join our mailing list! I send out cool and helpful JavaScript material on a regular basis, you’ll surely upgrade your JavaScript skills after a few weeks of my lessons!

{ 0 comments… add one }

Leave a Comment