Codecademy - JavaScript
Last updated
Last updated
Started with Codecademy Full stack engineer course to help understand web apps and dev better
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.
Return factorial of number
Categorising People by age
Categorising student's grades by average grades
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!’
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
and planet
expect earthWeight
to be a number
expect planet
to be a string
return 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.'
if else loops again
emoticon practice
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, and false
if the computer player wins.
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.
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
or const
. Even when declared with const
, arrays are still mutable. However, a variable declared with const
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.
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 for document.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.
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 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:
require()
is a function used to import modules from other files or Node packages.
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.
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
This ensures the variables are accessible in the REPL
npm/yarn = package manager for Node
replit = inbrowser IDE for projects
using built in object methods
Arrays have many methods that perform different tasks, such as .slice()
and .shift()
, you can find documentation at the website.
and for creating web servers.
, , and for interacting with the file system, operating system, and file/directory paths.
process
is an object referencing to the actual computer running a Node program and allows for access to command-line arguments and much more.