- Fork and Clone
- Create a new branch called:
yourName-ArrayChallenge - Install dependecies:
npm i(seepackage.json) - Use a whiteboard to work out a solution to the challenges below
- Translate the broad ideas to pseudo code
- Convert the pseudo code to real JavaScript code
- Type into your text editor the JavaScript code you've come up with one step at a time
- Work through your bugs.
- Use
node main.jsto run the program after every line of code you build.
.length- Create an array called
carswhich consists of 4 different types of cars as String type. The first car type should be Ford. - Console.log the length of the array.
- Use
node main.jsto run the program.
- Create an array called
.concat()- Create another array called
moreCarswith 4 more different types of cars. The last car type should be Honda. - Use the
concatmethod to combine thecarsandmoreCarsarrays into another array calledtotalCars. - Console.log the new array.
- Run the program.
- Create another array called
.indexOf()and.lastIndexOf()- Use the
indexOfmethod to console.log the index ofHondaintotalCars. - Use the
lastIndexOfmethod to console.log the index ofFordintotalCars. - Run the program.
- Use the
.join()- Use the
joinmethod to convert the arraytotalCarsinto a string calledstringOfCars. - Console.log
stringOfCars. - Run the program.
- Use the
.split()- Use the
splitmethod to convertstringOfCarsinto an array calledcarsFromString. - Console.log the array you just created.
- Run the program.
BONUS: Go back and pass a comma (
',') in as an argument to.split()to separate the cars into individual items in the array. This is called a separator and it can be any character you wish to separate strings by.- Use the
.reverse()- Use the
reversemethod to create an arraycarsInReversewhich is the arraytotalCarsin reverse. - Console.log
carsInReverse. - Run the program.
- Use the
.sort()- Use the
sortmethod to put the arraycarsInReverseinto alphabetical order. - Based on the types of cars you used, predict which item in the array should be at index 0.
- Use the following code to confirm or reject your prediction:
console.log(carsInReverse.indexOf('yourPrediction'));
- Use the
.slice()- Create a
petsarray by copy/pasting the following:const pets = ['dog', 'cat', 'fish', 'rabbit', 'snake', 'lizard', 'bird'] - Use the
slicemethod to create areptilesarray withsnakeandlizardfrom thepetsarray. - Console.log the
reptilesarray and run the program. - Now console.log the
petsarray and run the program. Why do you thinksnakeandlizardare still in the original array?
- Create a
.splice()- Create a new array called
removedReptiles, using thesplicemethod to removesnakeandlizardfrom thepetsarray. - Console.log
removedReptilesandpetsand run the program. - Go back and add the string
'hamster'in as a third parameter to yoursplicemethod, then run the program again and notice how thepetsarray has changed. Do you see how that works?
- Create a new array called
.pop()- Use the
popmethod to remove the last item from thepetsarray, saving it to a variable calledremovedPet. - Console.log
removedPetandpetsand run the program.
- Use the
.push()- Use the
pushmethod to addremovedPetback to the end of thepetsarray. - Console.log
petsand run the program.
- Use the
.shift()- Use the
shiftmethod to remove and console.log the first item in thepetsarray.
- Use the
.unshift()- Use the
unshiftmethod to add the string'turtle'as the first item in thepetsarray. - Console.log the
petsarray and run the program. If all went according to plan, you should see['turtle', 'cat', 'fish', 'rabbit', 'hamster', 'bird'].
- Use the
.forEach()- Create a numbers array by copy/pasting the following:
const numbers = [23, 45, 0 , 2, 8, 44, 100, 1, 3, 91, 34] - Write code that will add 2 to each item in the array
numbers.forEachrequires a function to be passed into it as its first argument.- Build a function called
addTwothat can take in num, index, and arr as parameters—(num, index, arr)—and returnsnum + 2at eachindexof thearr. - Use
.forEach()on thenumbersarray, passing in your freshly built functionaddTwoas an argument, in order to add 2 to each number in the array.
- Console.log
numbersand run the program.
- Create a numbers array by copy/pasting the following:
- Use repl.it to write the solution code first. (It's a faster environment vs using the
node main.jscommand over and over again.) - Use your documentation.
- Push yourself to learn on your own. Ask: How does this work?
- Clone, setup, testing, and running instructions for all projects is below
-
Click the 'Fork' button (choose your account if prompted).
-
Copy HTTPS URL from your forked repository
-
In your terminal/gitBash/CommandPrompt navigate (using
cd) into a directory where you want to start keeping your repositories. (/jsDevFolder) -
Clone your new repository by typing
git clone <forked clone URL>(the HTTPS URL you copied above) -
Now go into the new directory by using
cd project-repo -
Add the base repository as an upstream
git remote add upstream https://github.com/AustinCodingAcademy/<PROJECT-REPO>.git -
Check the configuration of your remotes with
git remote -v, it should look very similar to this (except it'll be YOUR username)
$ git remote -v
origin git@github.com:username/javascript-workbook.git (fetch)
origin git@github.com:username/javascript-workbook.git (push)
upstream git@github.com:AustinCodingAcademy/javascript-workbook.git (fetch)
upstream git@github.com:AustinCodingAcademy/javascript-workbook.git (push)-
From your project directory, run
npm ito tell NPM to install all the node modules we use in this class (seepackage.json) -
Use your textEditor (VS Code) to change your files.
-
When you're finished
git status, stage your filegit add ., commit your changesgit commit -m "functions working", and push to GitHubgit pushgit status git add . git commit -m "Initial Commit" git push origin gh-pages
-
Now go to your forked repository on GitHub (at https://github.com/your-username/javascript-workbook). A little yellow box should have popped up asking you to make a Pull Request. Click to review.
-
Click "Create Pull Request"
-
Every time you make a change and push to GitHub, this PR will automatically update. No need to do it more than once.
-
To get the latest code/homework/test updates, be sure to have a "clean working directory" by committing or removing all of your changes. You check for a "clean working environment" by running
git statusand making sure no files show up. -
Run
git pull upstream gh-pages
Simply run node path/to/file.js
example node 01week/rockPaperScissors.js
Tests are a great way to make sure your code works the way you planned it would, and to make sure you don't break something in the future. We will be using them to test our understanding of the lesson. It's also our main way to assign grades for an assignment.
To run the tests on a file run npm test path/to/file.js, etc.
Simply run npm run lint
- Run
npm start - To break out of the server, press
ctrl+c