Class 29 Flashcards
What is a program?
A set of instructions that you write to tell a computer what to do
What is a programming?
A task of writing instructions in a language that computer can understand
What does variables do?
Store information
How to create/declare a variable?
Use the keyword “let”
Conditional syntax
if (condition is true) {
//do this
else if (….) {
//do that
else {
// do this then
}
Multiple conditions (and)
if (name === “Leon” && status === “Balling”) {
do this
}
Multiple conditions (or)
if (day === “Saturday” || day === “Sunday”) {
It is the weekend
}
What are functions?
Reusable sets of instructions
Functions
function yell (word) {
alert(word)
}
yell(“Hello”)
What are loops?
Repeat an action some number of times
What are 3 main types of loops in js?
For, while and do while loops
For loop
for (let i = 1; i < 5; i++) {
console.log(i)
}
In order to create function in js, what keyword do you need?
function
Ex.
function toCelsius(temp) {
… }
What are arrays?
A data structure to store ordered collection
Array elements are numbered starting with zero
Arrays have many methods to manage the order of elements
Arrays are populated with any type of elements (string, boolean or number)
How to create arrays?
Through literal notation or a constructor
Declaring arrays through literal notation
let newArr = []
Array iteration
Iterate through an array passing in the value and index of each element
newArr.forEach((x,i) => console.log(x))
Array length
console.log (newArr.length)
Explain the reduce method of an array
Reduces an array into a single value by passing a callback function on each element of an array
Syntax of a reduce method of an array
const sum = array1.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue,
)
When to use array.reduce()?
Sum
What does filter method of an array do?
The filter() method creates a new array filled with elements that pass a test provided by a function.