- Open your command line and navigate to your
reposdirectory (if you do not have areposfolder, then you can usemkdir reposto create one) - Use this template repository to start a new project in your repos folder:
git clone <repo_name> - cd
repo_nameto navigate into your new repo directory - Start Visual Studio Code and select 'Open Folder'. Then select
repo_nameto open the folder in the editor (or just typecode .in your terminal inside the repo directory) - Follow the instructions on the README.md file to complete exercises
- Open the app.js file to get started
- The Array Object in JavaScript has a built-in
reducemethod that iterates over each value in an array and returns a value. It is very useful when calculating sums. - Write a function that takes an array of numbers as a parameter and returns the sum value of each number in the array. (i.e. write a custom Array.prototype.reduce() function)
- You can use the follow array:
const numbers = [2, 22, 12, 17, 18, 39, 129];
- Declare a function
arraySumthat takesnumbersas a parameter - Declare a variable
sumand initializesumas 0 - Inside the
arraySumfunction body, iterate over each number (value) in thenumbersarray using the array'sforEachmethod- The array
forEachmethod takes a callback (function) as a parameter, where you can pass in the value and index of the current array position numbers.forEach((number, index) => {...});
- The array
- Inside the body of the
forEachcallback function, write code so that each number in thenumbersarray is added to the value ofsum - Below the
forEachmethod, thenreturn sum; - Call the
arraySum(numbers)method insideconsole.logto print out the returned sum.
- Write a “Book” object. Your book object should have the book’s title, author, the number of pages, and whether or not you have read the book
- Declare a variable named
book, and assign an object ({}) to the variable. - Using dot notation:
- assign the title of your favorite book to a property named
title, - assign the number of pages to a property named
pages, - and assign the number of times you've read the book to a property named
readCount
- assign the title of your favorite book to a property named
- Using dot notation, add a method named
infoto thebookobject that returns a string that combines thetitle,pages, andreadCountas an informational string. Ex:The Hobbit by J.R.R Tolkien, 295 pages, read 3 times. - After you have added the properties and method to the object, call the
book.info()method insideconsole.logto print out the returned string.