Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Last active October 31, 2023 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prof3ssorSt3v3/b86cc52ebedefd90938659ebf838cf27 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/b86cc52ebedefd90938659ebf838cf27 to your computer and use it in GitHub Desktop.
JavaScript Practice Exercises

JavaScript Practice Exercises

1. Count Occurrences

Start with an array of integers. Eg: const arr = [1, 2, 3, 2]

Write a function that will loop through the array counting the number of occurences of each value. The return value of the function will be an Object whose keys will be the numbers from the array and whose values will be the occurrence count for each value. Eg: { 1:1, 2:2, 3:1 }

2. Find the Smallest

Start with an array of integers. Eg: const arr = [17, 8, -92, 12, 56, 44, -33]

Write a function that will loop through the array and find the smallest number in the array. The function will return the smallest number.

For variations of this try searching for the largest number, or find the smallest number that is not negative.

3. Remove Falsey Values

Start with an array filled with any kind of values. Eg: const arr = ['steve', false, null, 0, 1, 2, NaN, 'hello']

Write a function that will loop through the array and find any value that is falsey. The falsey values are false, 0, null, "", undefined and NaN. Do this without changing the original array. The function returns the new version of the array.

For variations of this try searching for and removing nullish values. Nullish means null or undefined.

4. Remove the Even Numbers

Start with an array filled with integers. Eg: const arr = [1, 2, 4, 5, 5, 9, 12]

Write a function that will loop through the array and find the even numbers. Create a new array that holds just the even numbers from the original array. The function should return the new array.

For variations of this try generating an array of the odd numbers, or counting the number of odd and the number of even numbers in the array and return an object with the two counts.

5. Calculate the Average

Start with an array filled with numbers. Eg: const arr = [1, 2, 3.14, 6.26, 99.99];

Write a function that calculates and returns the average value from the array.

For a variation try calculating and returning the median value. reference for what a median is

6. Reverse the Words

Write a function that accepts a single sentence as an argument and then returns the sentence with the words in reverse order. Eg: I am the one who knocks would become knocks who one the am I.

For a variation of this try checking for punctuation at the end of the sentence parameter and keep it at the end of the new sentence, or capitalizing the new first word of the returned sentence. Also change the first letter of the new last word to lowercase (unless the last word is a single letter like "I").

7. Capitalizing the First Letter

Write a function that will accept a single sentence as an argument and then return a new version of the sentence where the first letter of every word has been capitalized.

Eg: We're going to need a bigger boat becomes We're Going To Need A Bigger Boat

8. Truncating Strings

Write a function that takes a string and a maximum length as parameters. The function should truncate the string to the specified maximum length and add an ellipsis "..." at the end if it was truncated. For example, if the input string is "Hello, world!" and the maximum length is 5, the function should return "Hello...".

The unicode value for an ellipsis is \u2026. You can use that in a string.

9. Palindrome

Write a function that takes a string as a parameter and returns true if the string is a palindrome (reads the same backward as forward), and false otherwise. For example, if the input string is "hannah", the function should return true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment