# FizzBuzz

Write a program that uses `console.log` to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print `"Fizz"` instead of the number, and for numbers divisible by 5 (and not 3), print `"Buzz"` instead.

When you have that working, modify your program to print `"FizzBuzz"`, for numbers that are divisible by both 3 and 5 (and still print `"Fizz"` or `"Buzz"`for numbers divisible by only one of those).

Solution: The trick here is to use two if statements (because you want it to be able to return BOTH Fizz and Buzz — use if and else if when you want just one option or the other), and console.log and || at the end to print one or the other.

Advertisements

# Leap Year

Write a program which will ask for a starting year and an ending year, and then puts all of the leap years between them (and including them, if they are also leap years). Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years). Ran the program with years 1600 – 2000 and this is the results:

Enter the starting year:

>> 1600

Enter the ending year:

>> 2000

Here are the leap years:

1600

1604

1608

1612

1616

1620

1624

1628

1632

1636

1640

1644

1648

1652

1656

1660

1664

1668

1672

1676

1680

1684

1688

1692

1696

1704

1708

1712

1716

1720

1724

1728

1732

1736

1740

1744

1748

1752

1756

1760

1764

1768

1772

1776

1780

1784

1788

1792

1796

1804

1808

1812

1816

1820

1824

1828

1832

1836

1840

1844

1848

1852

1856

1860

1864

1868

1872

1876

1880

1884

1888

1892

1896

1904

1908

1912

1916

1920

1924

1928

1932

1936

1940

1944

1948

1952

1956

1960

1964

1968

1972

1976

1980

1984

1988

1992

1996

2000

# Caesar Cipher

Write a method that takes in an integer ‘offset’ and a string. Produce a new string, where each letter is shifted by ‘offset’. You may assume that the string contains only lowercase letters and spaces.

When shifting “z” by three letters, wrap around to the front of the alphabet to produce the letter “c”.  You may want to use the % modulo operation to handle wrapping of “z” to the front of the alphabet.

You’ll want to use String’s ‘ord’ method and Integer’s ‘chr’ method.  ‘ord’ converts a letter to an ASCII number code. ‘chr’ converts an ASCII number code to a letter.

You may look at the ASCII printable characters chart:

http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

Notice that the letter ‘a’ has code 97, ‘b’ has code 98, etc., up to ‘z’ having code 122.

The solution, annotated by me: How the code works, in an example: Second time solving this problem, it seemed to make less sense to me..  I couldn’t solve it on my own AND it took me quite a bit to decipher the solution.

Anyways, here’s the thought process:

1. Split string into array of words

2. Write two while loops, first one to iterate through each word in the string, and second one to iterate through each letter in each word.

3. In the while loop for each letter, determine the location of each character by subtracting its ASCII value by 97 (which is “a”).

4. Determine where its new location should be by adding the offset value to its current location.  Use % 26 to account for wrapping “y” and “z” to the front of the alphabet.

5. Set the current letter to its new ASCII character determined  by the new location/index, and convert it to regular alphabet letter.

Example: (3, “abc xyz”) => “def abc”

(The following notes show the work for letter z.) *If we didn’t use % 26, the new index for “z” would have been 28 which is more than the number of letters in the alphabet.

# Greatest Common Factor

Write a method that takes in two numbers. Return the greatest integer that evenly divides both numbers. You may wish to use the % modulo operation.

Solution #1: Provided solution: Still not sure why we have to start i at the smaller number in either solution.

Second time solving this, I had a much easier time  diving into the problem after outlining my thoughts first.

Thought process:

(number1, number 2) == biggest_common_factor

Example: (16, 24) == 8

Determine the limit of which you’ll run the while loop, which in this case is the smaller of the two numbers given. (Because we’re trying to find the biggest common number, and it can’t be anything bigger than either numbers).

upper limit = smaller number

Find all the common factors for both numbers.

number1 % common_factor == 0
number2 % common_factor == 0

16: 2, 4, 8, 16
24: 2, 4, 3, 6, 8, 12, 24

Compare all the common factors one by one and return the biggest one.

=> 8 NOTE: ‘i > biggest_common_factor’ is not necessary because it’s implied that ‘i’ will always be bigger since we’re incrementing.

# Dasherize Number

Write a method that takes in a number and returns a string, placing a single dash before and after each odd digit. There is one exception: don’t start or end the string with a dash.

You may wish to use the `%` modulo operation; you can see if a number
is even if it has zero remainder when divided by two. My approach to solving this problem:

• Convert given number to a string of numbers.   For example, 203 => “2 0 3”
• Convert each number in the new string to integer for boolean comparisons.
• Go through the new array of numbers with a while loop
• Set the number at the first index (idx = 0) as current digit.  (“2”)
• After index 0, if the previous digit or current digit is odd, add a dash “-“back to the new result and increment idx to go through next number in the array.   (“20-3”)

Second time solving this:

• I forgot to convert the number back to integer for boolean.
• Also didn’t know to add the numbers and dashes back into the string correctly.
• Very annoyed in myself for not being able to solve this.