From 260e657364628e80587595c2c6611bf89dd55e42 Mon Sep 17 00:00:00 2001 From: Andrej Rasevic <andrej@rasevicengineering.com> Date: Fri, 10 Jan 2025 17:07:13 -0500 Subject: [PATCH] adding exercise 2 --- Exercises/Exercise2/Exercise2_Description.md | 19 + Exercises/Exercise2/index.js | 51 +++ Exercises/Exercise2/presidents.js | 407 +++++++++++++++++++ 3 files changed, 477 insertions(+) create mode 100644 Exercises/Exercise2/Exercise2_Description.md create mode 100644 Exercises/Exercise2/index.js create mode 100644 Exercises/Exercise2/presidents.js diff --git a/Exercises/Exercise2/Exercise2_Description.md b/Exercises/Exercise2/Exercise2_Description.md new file mode 100644 index 0000000..ec31dea --- /dev/null +++ b/Exercises/Exercise2/Exercise2_Description.md @@ -0,0 +1,19 @@ +# Exercise 2: + +## Due Date: Friday January 12 2024, 11:59 PM EST + +## Objectives: To get familiar with javascript functional prototypes, template strings, arrays and higher order javascript array functions and node modules. + +## Specifications/Requirements + +1. Description: +Â Â To complete this exercise you will be working in the 2 source files we have provided you:Â index.js and presidents.js. In presidents.js we have given you an array containing information about the first 45 presidents of America. You need to export this array so other node modules can use it. In `index.js` we have given you a main function that you need to complete. You will be using the javascript functional prototype provided in the comments in order to obtain the required data and display it in the console using template strings to format output. You will be using the array defined in presidents.js as the array you will be acting upon inside of main. Â + +2. Formating and output: +Â Â You will be logging all your results to the console, and the specification as to how the ouput should look are included in the comments above each exercise. If there are no specifications for an exercises output you may assume that there is no special formating associated with that exercise. + +3. Additional Notes: +Â Â You should not alter the presidents data we provide you with. This should go without saying, but you should not hardcode your results, we will check your submissions to make sure this does not happen. You should use the functional array prototypes that we have gone over in class and that are mentioned in this exercise (map, sort, filter, reduce), using for loops, for..of loops, while loops, etc will result in a substantial amount of point deduction. + +4. Submission: + To deliver your submission you will need to commit your changes locally and push to your repo on the university gitlab server. You __MUST__ verify that your push was successful by logging into gitlab and verifying that you can see your files. \ No newline at end of file diff --git a/Exercises/Exercise2/index.js b/Exercises/Exercise2/index.js new file mode 100644 index 0000000..abb3dcb --- /dev/null +++ b/Exercises/Exercise2/index.js @@ -0,0 +1,51 @@ +function main() { + // Part 1 Questions + //================================================================================================ + + // Array.prototype.filter() + // 1. Filter the list of presidents for those who were born in the 1800's + let result1; + + console.table(result1); + + // Array.prototype.map() + // 2. Map the presidents name and party to a seperate array. Format: <president_name> (<party>) + + let result2; + + console.table(result2); + + // Array.prototype.sort() + // 3. Sort the presidents by birthdate, by descending order (most recent to least recent dates) + + let result3; + + console.table(result3); + + // Array.prototype.reduce() + // 4. How many years combined did all the presidents live? (Hint: Lookout for the presidents who are still alive) + + let result4; + + console.log(`Number of combined years presidents have lived: ${result4}`); + + // sort/map Exercise + // 5. Sort the presidents names by name in ascending order, and map the time they took office to a new list. Format: <president_name> - <took_office> + + let result5; + + console.table(result5); + + // Array.prototype.reduce() + // Hint: to check if a property exists in an object you can simply + // have the following check: if (obj.property_name) + // if the property exists on the obj object that will return true + // Additonally, reduce can take an empty object as its accumulator. + // 6. Sum up the number of times each political party held the presidency + + let result6; + + console.table(result6); +} + +main() \ No newline at end of file diff --git a/Exercises/Exercise2/presidents.js b/Exercises/Exercise2/presidents.js new file mode 100644 index 0000000..7c2d054 --- /dev/null +++ b/Exercises/Exercise2/presidents.js @@ -0,0 +1,407 @@ +const presidents = [ + { + number: 1, + president: "George Washington", + birth_year: 1732, + death_year: 1799, + took_office: "1789-04-30", + left_office: "1797-03-04", + party: "No Party" + }, + { + number: 2, + president: "John Adams", + birth_year: 1735, + death_year: 1826, + took_office: "1797-03-04", + left_office: "1801-03-04", + party: "Federalist" + }, + { + number: 3, + president: "Thomas Jefferson", + birth_year: 1743, + death_year: 1826, + took_office: "1801-03-04", + left_office: "1809-03-04", + party: "Democratic-Republican" + }, + { + number: 4, + president: "James Madison", + birth_year: 1751, + death_year: 1836, + took_office: "1809-03-04", + left_office: "1817-03-04", + party: "Democratic-Republican" + }, + { + number: 5, + president: "James Monroe", + birth_year: 1758, + death_year: 1831, + took_office: "1817-03-04", + left_office: "1825-03-04", + party: "Democratic-Republican" + }, + { + number: 6, + president: "John Quincy Adams", + birth_year: 1767, + death_year: 1848, + took_office: "1825-03-04", + left_office: "1829-03-04", + party: "Democratic-Republican" + }, + { + number: 7, + president: "Andrew Jackson", + birth_year: 1767, + death_year: 1845, + took_office: "1829-03-04", + left_office: "1837-03-04", + party: "Democratic" + }, + { + number: 8, + president: "Martin Van Buren", + birth_year: 1782, + death_year: 1862, + took_office: "1837-03-04", + left_office: "1841-03-04", + party: "Democratic" + }, + { + number: 9, + president: "William Henry Harrison", + birth_year: 1773, + death_year: 1841, + took_office: "1841-03-04", + left_office: "1841-04-04", + party: "Whig" + }, + { + number: 10, + president: "John Tyler", + birth_year: 1790, + death_year: 1862, + took_office: "1841-04-04", + left_office: "1845-03-04", + party: "Whig" + }, + { + number: 11, + president: "James K. Polk", + birth_year: 1795, + death_year: 1849, + took_office: "1845-03-04", + left_office: "1849-03-04", + party: "Democratic" + }, + { + number: 12, + president: "Zachary Taylor", + birth_year: 1784, + death_year: 1850, + took_office: "1849-03-04", + left_office: "1850-07-09", + party: "Whig" + }, + { + number: 13, + president: "Millard Fillmore", + birth_year: 1800, + death_year: 1874, + took_office: "1850-07-09", + left_office: "1853-03-04", + party: "Whig" + }, + { + number: 14, + president: "Franklin Pierce", + birth_year: 1804, + death_year: 1869, + took_office: "1853-03-04", + left_office: "1857-03-04", + party: "Democratic" + }, + { + number: 15, + president: "James Buchanan", + birth_year: 1791, + death_year: 1868, + took_office: "1857-03-04", + left_office: "1861-03-04", + party: "Democratic" + }, + { + number: 16, + president: "Abraham Lincoln", + birth_year: 1809, + death_year: 1865, + took_office: "1861-03-04", + left_office: "1865-04-15", + party: "Republican" + }, + { + number: 17, + president: "Andrew Johnson", + birth_year: 1808, + death_year: 1875, + took_office: "1865-04-15", + left_office: "1869-03-04", + party: "Democratic" + }, + { + number: 18, + president: "Ulysses S. Grant", + birth_year: 1822, + death_year: 1885, + took_office: "1869-03-04", + left_office: "1877-03-04", + party: "Republican" + }, + { + number: 19, + president: "Rutherford B. Hayes", + birth_year: 1822, + death_year: 1893, + took_office: "1877-03-04", + left_office: "1881-03-04", + party: "Republican" + }, + { + number: 20, + president: "James A. Garfield", + birth_year: 1831, + death_year: 1881, + took_office: "1881-03-04", + left_office: "1881-09-19", + party: "Republican" + }, + { + number: 21, + president: "Chester A. Arthur", + birth_year: 1829, + death_year: 1886, + took_office: "1881-09-19", + left_office: "1885-03-04", + party: "Republican" + }, + { + number: 22, + president: "Grover Cleveland", + birth_year: 1837, + death_year: 1908, + took_office: "1885-03-04", + left_office: "1889-03-04", + party: "Democratic" + }, + { + number: 23, + president: "Benjamin Harrison", + birth_year: 1833, + death_year: 1901, + took_office: "1889-03-04", + left_office: "1893-03-04", + party: "Republican" + }, + { + number: 24, + president: "Grover Cleveland", + birth_year: 1837, + death_year: 1908, + took_office: "1893-03-04", + left_office: "1897-03-04", + party: "Democratic" + }, + { + number: 25, + president: "William McKinley", + birth_year: 1843, + death_year: 1901, + took_office: "1897-03-04", + left_office: "1901-09-14", + party: "Republican" + }, + { + number: 26, + president: "Theodore Roosevelt", + birth_year: 1858, + death_year: 1919, + took_office: "1901-09-14", + left_office: "1909-03-04", + party: "Republican" + }, + { + number: 27, + president: "William Howard Taft", + birth_year: 1857, + death_year: 1930, + took_office: "1909-03-04", + left_office: "1913-03-04", + party: "Republican" + }, + { + number: 28, + president: "Woodrow Wilson", + birth_year: 1856, + death_year: 1924, + took_office: "1913-03-04", + left_office: "1921-03-04", + party: "Democratic" + }, + { + number: 29, + president: "Warren G. Harding", + birth_year: 1865, + death_year: 1923, + took_office: "1921-03-04", + left_office: "1923-08-02", + party: "Republican" + }, + { + number: 30, + president: "Calvin Coolidge", + birth_year: 1872, + death_year: 1933, + took_office: "1923-08-02", + left_office: "1929-03-04", + party: "Republican" + }, + { + number: 31, + president: "Herbert Hoover", + birth_year: 1874, + death_year: 1964, + took_office: "1929-03-04", + left_office: "1933-03-04", + party: "Republican" + }, + { + number: 32, + president: "Franklin D. Roosevelt", + birth_year: 1882, + death_year: 1945, + took_office: "1933-03-04", + left_office: "1945-04-12", + party: "Democratic" + }, + { + number: 33, + president: "Harry S. Truman", + birth_year: 1884, + death_year: 1972, + took_office: "1945-04-12", + left_office: "1953-01-20", + party: "Democratic" + }, + { + number: 34, + president: "Dwight D. Eisenhower", + birth_year: 1890, + death_year: 1969, + took_office: "1953-01-20", + left_office: "1961-01-20", + party: "Republican" + }, + { + number: 35, + president: "John F. Kennedy", + birth_year: 1917, + death_year: 1963, + took_office: "1961-01-20", + left_office: "1963-11-22", + party: "Democratic" + }, + { + number: 36, + president: "Lyndon B. Johnson", + birth_year: 1908, + death_year: 1973, + took_office: "1963-11-22", + left_office: "1969-01-20", + party: "Democratic" + }, + { + number: 37, + president: "Richard Nixon", + birth_year: 1913, + death_year: 1994, + took_office: "1969-01-20", + left_office: "1974-08-09", + party: "Republican" + }, + { + number: 38, + president: "Gerald Ford", + birth_year: 1913, + death_year: 2006, + took_office: "1974-08-09", + left_office: "1977-01-20", + party: "Republican" + }, + { + number: 39, + president: "Jimmy Carter", + birth_year: 1924, + death_year: null, + took_office: "1977-01-20", + left_office: "1981-01-20", + party: "Democratic" + }, + { + number: 40, + president: "Ronald Reagan", + birth_year: 1911, + death_year: 2004, + took_office: "1981-01-20", + left_office: "1989-01-20", + party: "Republican" + }, + { + number: 41, + president: "George H. W. Bush", + birth_year: 1924, + death_year: 2018, + took_office: "1989-01-20", + left_office: "1993-01-20", + party: "Republican" + }, + { + number: 42, + president: "Bill Clinton", + birth_year: 1946, + death_year: null, + took_office: "1993-01-20", + left_office: "2001-01-20", + party: "Democratic" + }, + { + number: 43, + president: "George W. Bush", + birth_year: 1946, + death_year: null, + took_office: "2001-01-20", + left_office: "2009-01-20", + party: "Republican" + }, + { + number: 44, + president: "Barack Obama", + birth_year: 1961, + death_year: null, + took_office: "2009-01-20", + left_office: "2017-01-20", + party: "Democratic" + }, + { + number: 45, + president: "Donald J. Trump", + birth_year: 1946, + death_year: null, + took_office: "2017-01-20", + left_office: null, + party: "Republican" + } + ]; \ No newline at end of file -- GitLab