ga

wdi-10-chi (fluff hounds)

LA FORGE

Homework 3: Problem Solving with functions


Assigned: Saturday, Dec 9, 2017
Adapted by Reuben Ayres from homework assignment created by Thom Page


Due: Monday, December 11, 2017 @8:00 pm.


Setup

Make sure you are finished with the Lab we started in class on Friday. Use slack if you have questions.

Fork this repository, and then from the github page for your fork, clone that forked repository onto your machine. As indicated, do a commit after each step is completed. Of course, you may make more than one commit per problem (like, if you finish a part of a problem).

After your write each function, make sure they work as expected by calling the function for a few test cases. Include any from the problems themselves and one or two of your own. I recommend commenting out completed work as you go, but when you finish the assignment, uncomment all your code amd make sure it still works, and that the console output is meaningful.

Like so:

console.log("2. Palindrome:");
console.log('Radar: ' + checkPalindrome("Radar"));
console.log('Borscht: ' + checkPalindrome("Borscht"));
console.log('Sit on a potato pan otis: ' + checkPalindrome("Sit on a potato pan otis"));
console.log('Tennessee: ' + checkPalindrome("Tennessee"));

You should understand every letter of code in your submitted responses well enough to explain them to someone else.

Submission:

When you are finished, submit a pull request from your fork.


Using Geordi La Forge as your muse, you're gonna write some functions.

Good luck :) you got this!!! :)


1. Verbal questions

Write answers to the following questions as comments. As a reminder you can highlight your code and type command + / to turn your code into a comment.

// my commented answer

a. What is the difference between a parameter and an argument?

b. Within a function, what is the difference between return and console.log?

c. Explain the concept of scope in four (or fewer) concise sentences.


🔴 **Commit your work.**
The commit message should read:
"Finished #1: Verbal questions".

2. Palindrome

Write a function checkPalindrome that accepts a single argument, a string. The function should return true (Boolean) if the string is a palindrome, false if it is not. Make sure your function will give the correct answer for words with capital letters. Additionally, make sure that spaces are ignored.

console.log(checkPalindrome("Radar"));
 => true
console.log(checkPalindrome("Borscht"));
=> false

🔴 **Commit your work.**
The commit message should read:
"Finished #2 - Palindrome".

3. Digit Sum

Write a function sumDigits that accepts a number and returns the sum of its digits.

console.log(sumDigits(42));
=> 6;

🔴 **Commit your work.**
The commit message should read:
"Finished #3 - Digit Sum".

4. Pythagoras

Write a function calculateSide that takes two arguments: sideA and sideB, and returns the solution for sideC using the Pythagorean theorem.

hint: discover the Pythagorean Theorem on a website called google.com
hint: checkout the Math methods in javascript

console.log(calculateSide(8, 6));
=> 10

🔴 **Commit your work.**
The commit message should read:
"Finished #4 - Pythagoras".

5. Sum Array

Write a function sumArray that takes an array as an argument. The array should contain numbers. The function should return the sum of the numbers in the array. Use a for loop within the function to iterate over the array and sum the contents. Use a variable such as

let sum = 0;

that will accumulate value within the loop. Expected result:

console.log(sumArray([1, 2, 3, 4, 5, 6]));
=> 21

🔴 **Commit your work.**
The commit message should read:
"Finished #5 - Sum Array".

6. Prime Numbers

A Prime number is a number that is not evenly divisible by another number except 1 and itself. If you want to read more deeply about it, go here. To test whether a number is Prime, you only need to test as far as the square root of that number. This is advisable for optimization and testing large numbers.

Step One

Write a function called checkPrime that will test whether a number is Prime. The function will return true (Boolean) if Prime, false if not. Hint: Check every number up to the square root. To do this, try a for loop.

Step Two

Write another function called printPrimes that will print (console log) all the Primes up to an arbitrary limit. For example, if you invoke your function with printPrimes(97), it will print all the Prime numbers up to and including 97. This function can call the previous checkPrime function.


🔴 **Commit your work.**
The commit message should read:
"Finished #6 - Prime Numbers".

7. Insert Dash

Write a function insertDash that accepts a number as a parameter and returns a string with a dash inserted between any consecutive odd numbers. There should not be a dash at the end, it goes only between numbers.

console.log(insertDash(454793));

=> 4547-9-3


🔴 **Commit your work.**
The commit message should read:
"Finished #7 - Insert Dash".

Hungry for more?

These are optional and a little trickier. Use Google, MDN, stackoverflow, etc as needed.

However, If you straight up copy code from a tutorial or stack overflow post, make sure that you 1) understand exactly how it works such that you could explain it to someone else and 2) put a comment above the code you took saying where you got it and how it works. Like so:

// This string reverse function is totally sick, bra.  
// I took it from http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/
// It uses method chaining to send output of the String.prototype.split() library 
// function to the .reverse() and then to the .reverse() and .join() Array library 
// functions, respectively.  I am inspired by its elegance.
function reverse(s) {
  return s.split('').reverse().join('');
}
  1. Read about the Array method Array.prototype.reduce() on MDN. Create a new answer to the "Sum Array" problem above using the method. We will talk about array helper methods more in class next week.

  2. Create a second palindrome function, fancyPalindrome such that not only spaces are ignored, but also any character that is not a letter of the English alphabet. In other words, your function should still return true if you pass in "R@d@r" or "Sit! (on a potato pan), Otis!!!" or "aromora12345" as arguments.

  3. Try to come up with a second reverseString method, reverseString2 that does not use .split(), .reverse(), or .join(). Spend some time thinking about how you might do it. There are many many other ways to do it. If you are still pondering, see the hints below; and if you're still pondering after that, see if you can come up with a solution using only MDN as a resource.

Hint: You can index into a string with brackets just like you can an array
Hint: Check out substr() and/or substring() and/or charAt()

  1. Read about CSS pseudo classes and pseudo elements on MDN.

a) Verbal questions: What are pseudo classes? What is the difference between pseudo classes and pseudo elements?

b) Without using JavaScript, adapt the layout from Friday so that when you mouse over each block it changes to a different color. The colors for unique elements should be unique but if there are repeated elements (such as the small "nav" boxes on the top right, use "nth-child" to make the even ones mouse to one color and the odd ones mouse to another color


🔴 **Commit your work.**
The commit message should read:
"Finished #8 - Hungry for More".

Now go play in the snow and/or have a beer and/or watch some Star Trek: The Next Generation and/or sleep and/or have coffee with a friend and/or call your mother. Something that isn't coding.