Codecademy - JavaScript
Started with Codecademy Full stack engineer course to help understand web apps and dev better
1. JavaScript Syntaxes I
Voting Rights Validator by age
The most common minimum age to vote is 18. Write a function canIVote()
that takes in a number, representing the person’s age, and returns the boolean true
if they are 18 years old or older, and the boolean false
if they are not.
While loop
Return factorial of number
If else statements
Categorising People by age
Task2:
Categorising student's grades by average grades
Taking inputs
Write a function, reportingForDuty()
, that has two string parameters, rank
and lastName
, and returns a string in the following format: ‘rank lastName reporting for duty!’
Arithmetic if else
Though an object’s mass remains consistent throughout the universe, weight is determined by the force of gravity on an object. Since different planets have different gravity, the same object would weigh different amounts on each of those planets! Cool, huh?
Write a function, calculateWeight()
. It should:
have two parameters:
earthWeight
andplanet
expect
earthWeight
to be a numberexpect
planet
to be a stringreturn a number representing what that Earth-weight would equate to on the
planet
passed in.
Use parseInt( ...) to convert to integer
String(...) to convert to string
Handle the following cases:
'Mercury'
weight = earthWeight
* 0.378
'Venus'
weight = earthWeight
* 0.907
'Mars'
weight = earthWeight
* 0.377
'Jupiter'
weight = earthWeight
* 2.36
'Saturn'
weight = earthWeight
* 0.916
For all other inputs, return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
howold()
if else loops again
emoticon practice
Project 1 : Number Guesser
Create a compareGuesses()
function. This function will be called each round to determine which guess is closest to the target number.
This function:
Has three parameters representing the user (human) guess, a computer guess, and the secret target number to be guessed.
Determines which player (human or computer) wins based on which guess is closest to the target. If both players are tied, the human user should win.
Return
true
if the human player wins, andfalse
if the computer player wins.
Capitalize Words in String
2. JavaScript Syntaxes II
Advanced Objects
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects
Object.assign() - takes original object + your additions
Object.keys() - takes the keys
Object.entries() -will also return an array, but the array will contain more arrays that have both the key and value of the properties in an object
Arrays Review:
Arrays are lists that store data in JavaScript.
Arrays are created with brackets
[]
.Each item inside of an array is at a numbered position, or index, starting at
0
.We can access one item in an array using its index, with syntax like:
myArray[0]
.We can also change an item in an array using its index, with syntax like
myArray[0] = 'new string'
;Arrays have a
length
property, which allows you to see how many items are in an array.Arrays have their own methods, including
.push()
and.pop()
, which add and remove items from an array, respectively.Arrays have many methods that perform different tasks, such as
.slice()
and.shift()
, you can find documentation at the Mozilla Developer Network website.Some built-in methods are mutating, meaning the method will change the array, while others are not mutating. You can always check the documentation.
Variables that contain arrays can be declared with
let
orconst
. Even when declared withconst
, arrays are still mutable. However, a variable declared withconst
cannot be reassigned.Arrays mutated inside of a function will keep that change even outside the function.
Arrays can be nested inside other arrays.
To access elements in nested arrays chain indices using bracket notation.
Using JavaScript on the Console ( CTRL + SHIFT + J)
Rather than typing
document.querySelector()
to select an element, you can type$()
. This syntax is inspired by jQuery, but it's not actually jQuery. It's just an alias fordocument.querySelector()
.debug(function)
effectively sets a breakpoint on the first line of that function.keys(object)
returns an array containing the keys of the specified object.
3. Node.js & its Global Objects
for writing server side JS code that runs in a computer itself instead of a browser
is written in C, C++ and JavaScript, built on open-source V8 JS engine which powers chrome (JS browser)
Node Global Objects
Node provides access to several important global objects for use with Node program files. When writing a file that will run in a Node environment, these variables will be accessible in the global scope of your file.
module
is an object referring to the functionality that will be exported from a file. In Node, each file is treated as a module, including:File System, OS, and Path for interacting with the file system, operating system, and file/directory paths.
require()
is a function used to import modules from other files or Node packages.process
is an object referencing to the actual computer process running a Node program and allows for access to command-line arguments and much more.
In this example, we are using Node’s built-in fs
module to read a script.js file. The callback function is called after the file-reading operation is completed. If an error occurred, it will be passed in as error
and thrown. If it doesn’t exist, the retrieved data from the file reading operation is logged to the console.
Running Node in Computer
Must be in same folder as the js script
Node as a REPL (Read Eval Print Loop)
run REPL by typing node and press enter
interactive JavaScript environment
Use .load to load JS files (existing) into the REPL
This ensures the variables are accessible in the REPL
npm/yarn = package manager for Node
replit = inbrowser IDE for projects
Last updated