If, Functions, Variables.
If, Functions, Variables.
https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://replit.com/
https://caniuse.com/
https://stackoverflow.com/
https://dev.to/
https://css-tricks.com/
https://www.w3.org/TR/CSS2/selector.html
https://developer.mozilla.org/en-US/
Console refers to an object. console.log() whatever is inside the parentheses will get printed or logged to
the console.
+ - % / %remainder.
string interpolation uses the $ equation. String concenation is the math symbols.
const kelvin = 0;
newton = Math.floor(celsius);
// Setting farenheit.
fahrenheit = Math.floor(fahrenheit);
----------------------------------------------
earlyYears *= 10.5;
If statements.
if (true) {
Instead of using true or false you can use a variable name which is set to true or false.
if (false) {
} else {
truthy values exist, falsy values are empty strings, nulls, 0s,
let defaultName;
if (username) {
defaultName = username;
} else {
defaultName = 'Stranger';
if (isNightTime) {
} else {
switch (groceryItem) {
case 'tomato':
console.log('Tomatoes are $0.49');
break;
case 'lime':
break;
case 'papaya':
break;
default:
console.log('Invalid item');
break;
--------------------------------------
switch(athleteFinalPosition){
break;
break;
break;
default:
break;
}}------------------------------------
----------------------------------------------------------------------------------------------------------------
Eightball project....
switch (randomNumber) {
case 0:
eightBall='It is certain'
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
console.log('Most Definately')
break;
console.log(eightBall)
---------------------------------------------------------------------------------------------------------
raceNumber += 1000;
} else {
---------------------------------------------------------------------------------------------------
Functions.
When first learning how to calculate the area of a rectangle, there’s a sequence of steps to calculate the
correct answer:
A function body, or the block of statements required to perform a specific task, enclosed in the
function’s curly brackets, { }.
function greetWorld() {
console.log('Hello, World!');
}
Hoisting is the act of calling the function before it is declared as shwon above ^.
To call a function in your code, you type the function name followed by parentheses.
greetWorld();
}
Assigning default values to function parameters. in this case it will default item 1 to milk, 2 to bread, and
so on.
console.log(numOfMonitors);
In this example we set the constant of numOfMonitors using the monitorCount function and passing it
an equation.
Helper Functions
We can also use the return value of a function inside another function. These functions being called
within another function are often referred to as helper functions. Since each function is carrying out a
specific task, it makes our code easier to read and debug if necessary.
function multiplyByNineFifths(number) {
function getFahrenheit(celsius) {
};
getFahrenheit(15); // Returns 59
console.log(totalCost);
return true;
else {
return false;
Arrow functions remove the need to type out the keyword function every time you need to create a
function. Instead, you first include the parameters inside the ( ) and then add an arrow => that points to
the function body surrounded in { } like this:
return area;
};
-------------------------------------------------------
So if we have a function:
};
-----------------------------------------------------------
};
would then become
userInput = userInput.toLowerCase();
return userInput;
return userInput;
else {
console.log('Error!');
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
} else {
} else {
} else {
}
}
console.log(determineWinner(userChoice, computerChoice));
playGame();
-------------------------------------------------------------------------------------------------------------------
return 8;
return 8;
return 7;
return 7;
} else if (day === 'friday') {
return 6;
return 6;
return 9;
var idealHours = 7;
return idealHours * 7;
console.log(`The user got more sleep than needed by ${actualSleepHours - idealSleepHours} hours`)
console.log(`The user should get some rest, they are behind ${idealSleepHours - actualSleepHours}
hours`)
}
calculateSleepDebt();
-------------------------------------------------------------------------------------------------------------------
return 'baby'
return 'child'
return 'teen'
return 'adult'
} else {
if((num1 < 0 || num1 > 100) || (num2 < 0 || num2 > 100) || (num3 < 0 || num3 > 100)){
return 'F';
return 'D';
return 'C';
return 'B';
return 'A';
switch (planet) {
case 'Mercury':
break;
case 'Venus':
break;
case 'Mars':
break;
case 'Jupiter':
return earthWeight * 2.36;
break;
case 'Saturn':
break;
default:
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
if (value) {
return true
return false
3 string paramters and string interpolation const sillySentence = (string1, string2, string3) => {
return `I am so ${string1} because I ${string2} coding! Time to write some more awesome ${string3}!`;
age calculator.
/*
Our solution is written as a function expression and uses string interpolation, but it would be equally
acceptable to use a function declaration and/or string concatenation
*/
return `The year ${year} was ${-newAge} years before you were born`
} else {
tip calculator.
// Write your function here:
switch(quality) {
case 'bad':
case 'ok':
case 'good':
default:
// Uncomment the line below when you're ready to try out your function
// We encourage you to add more function calls of your own to test your code!
switch(string) {
case 'shrug':
return '|_{"}_|';
return ':)';
return ':(';
case 'winky face':
return ';)';
case 'heart':
return '<3';
default:
create a function that returns true if number is even and false if odd.
// Write function below
if (num1 % 2 === 0) {
return true;
} else {
return false;
console.log(isEven(6));
console.log(numberDigits(0))
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
var targetNumber;
return true;
} else {
return false;
humanScore + 1;
computerScore + 1;
currentRoundNumber + 1;
console.log(compareGuesses(9, 2, generateTarget()));
console.log(targetNumber);