- 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.js
to run the program after every line of code you build.
.length
- Create an array called
cars
which 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.js
to run the program.
- Create an array called
.concat()
- Create another array called
moreCars
with 4 more different types of cars. The last car type should be Honda. - Use the
concat
method to combine thecars
andmoreCars
arrays into another array calledtotalCars
. - Console.log the new array.
- Run the program.
- Create another array called
.indexOf()
and.lastIndexOf()
- Use the
indexOf
method to console.log the index ofHonda
intotalCars
. - Use the
lastIndexOf
method to console.log the index ofFord
intotalCars
. - Run the program.
- Use the
.join()
- Use the
join
method to convert the arraytotalCars
into a string calledstringOfCars
. - Console.log
stringOfCars
. - Run the program.
- Use the
.split()
- Use the
split
method to convertstringOfCars
into 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
reverse
method to create an arraycarsInReverse
which is the arraytotalCars
in reverse. - Console.log
carsInReverse
. - Run the program.
- Use the
.sort()
- Use the
sort
method to put the arraycarsInReverse
into 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
pets
array by copy/pasting the following:const pets = ['dog', 'cat', 'fish', 'rabbit', 'snake', 'lizard', 'bird']
- Use the
slice
method to create areptiles
array withsnake
andlizard
from thepets
array. - Console.log the
reptiles
array and run the program. - Now console.log the
pets
array and run the program. Why do you thinksnake
andlizard
are still in the original array?
- Create a
.splice()
- Create a new array called
removedReptiles
, using thesplice
method to removesnake
andlizard
from thepets
array. - Console.log
removedReptiles
andpets
and run the program. - Go back and add the string
'hamster'
in as a third parameter to yoursplice
method, then run the program again and notice how thepets
array has changed. Do you see how that works?
- Create a new array called
.pop()
- Use the
pop
method to remove the last item from thepets
array, saving it to a variable calledremovedPet
. - Console.log
removedPet
andpets
and run the program.
- Use the
.push()
- Use the
push
method to addremovedPet
back to the end of thepets
array. - Console.log
pets
and run the program.
- Use the
.shift()
- Use the
shift
method to remove and console.log the first item in thepets
array.
- Use the
.unshift()
- Use the
unshift
method to add the string'turtle'
as the first item in thepets
array. - Console.log the
pets
array 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
.forEach
requires a function to be passed into it as its first argument.- Build a function called
addTwo
that can take in num, index, and arr as parameters—(num, index, arr)
—and returnsnum + 2
at eachindex
of thearr
. - Use
.forEach()
on thenumbers
array, passing in your freshly built functionaddTwo
as an argument, in order to add 2 to each number in the array.
- Console.log
numbers
and 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.js
command 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 [email protected]:username/javascript-workbook.git (fetch)
origin [email protected]:username/javascript-workbook.git (push)
upstream [email protected]:AustinCodingAcademy/javascript-workbook.git (fetch)
upstream [email protected]:AustinCodingAcademy/javascript-workbook.git (push)
-
From your project directory, run
npm i
to 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 push
git 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 status
and 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