JavaScript · Viking Code School

Basic JavaScript Algorithms

The first assignment in the “Falling in Love with JavaScript” course of Viking Code School‘s advanced prep work was made up of two parts. The first portion provided a series of problems or algorithms to solve for by writing a JavaScript function. Below I have listed each of the algorithms to be solved, followed by the code I wrote in JavaScript for them. Each of the functions were included in an object called sprintFunctions. My GitHub repo for this project with the JavaScript code can be found here. An HTML page here asks the user for the input for each of the functions then displays the results.

  1. largestEl takes an array and returns the largest element.
  2. reversed takes a string and reverses it.
  3. loudSnakeCase takes a full sentence and puts it into “Loud_Snake_Case” format but strips out any non-character elements (like punctuation).
  4. fizzBuzz takes an input of a number and returns an array containing all the elements from 1 to that number. Each element divisible by 3 is replaced by “FIZZ”, each element divisible by 5 is replaced by “BUZZ” and each element divisible by both 3 and 5 is replaced by “FIZZBUZZ”. Eg. fizzBuzz(6) => [ 1, 2, “FIZZ”, 4, “BUZZ”, “FIZZ” ]
  5. compareArrays takes two arrays and checks to see if they are equal (same contents in the same order). Assume they’re not nested.
  6. myMap takes an array and a function. It passes every element from the array into that function, in turn, running the function. The returned array should be filled with each of the return statements from that function.
  7. primes takes a number and outputs an array containing all prime numbers that occur prior to that number, e.g. primes(8) => [2,3,5,7]

basic-JS-algs

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s