CSD 360 - JS Collection Counters

Mod 9 Assignment

This assignment uses a function that counts the number of negative, zero, and positive numbers in a generated array. The user is allowed to either input their own array or to user a randomly generated array. The script starts by initializing several variables including two empty arrays, userNumbersArray and randomArray, and several elements from the HTML DOM using document.getElementById(). It also sets the variable randSelection to false which serves as a flag that is set to true when the user chooses to use a randomly generated array.


The displayArray() function takes an array as a parameter and displays its contents in the arrayDiv element. The addUserNumbers() function takes in user input numbers from the numbers input field, checks if they are valid integers, and pushes them into the userNumbersArray. It then calls the displayArray() function to display the generated array to the user.


The getRandomArray() function generates a random array of numbers and displays it to the user using the same displayArray() function. The counterResultFromDiv() function calls the counter() function dependent on the type of array the user is working with. The counter() function takes an array as a parameter and uses counter variables and a for loop to count keep track of the specific number occurances to be returned. The counter() function also makes use of a key value pair collection where each key will corresponding to the counter variable it describes. The results of the counter function are then displayed in the results element.

Array Counter
Enter Numbers to Generate Your Own Array
Enter numbers: