Published
Edited
Feb 25, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
for(var i=0; i<10; i++){
console.log(`Iteration number ${i}`) // Remember that the output will show up in the Console.
}
return i
}

//for every element in my array, perform something
Insert cell
Insert cell
{
for ( var val = 0; val<20; val++) {
//this is what gets updated within the for lopp. gets iterated in cosole. (see inspect)//
console.log('Hello World + ', val ,'goodbye')
}
//print the last value I get in my loop
return val

}

//note that we used var instead of let so that the content can be accessed within the block of code
//${} was used before becuase it lets us concatenate strings with whatever variables were out there ***

//make sure to see the console (inspect) because this is where the iteration is happening.

// start with {}
//then for
//then an argument for for with open ()// we will use let to assign a name of a varibale taht is going to be assigned as the loop goes through the elements
//our first value in this loop is zero
// ; then we need a stop condition. Lets count up to 20 . so val has to be less thn 20. if its greater than 20, could kill the client because then it will be an infinite loop. exceeds the stop condition (which we have in our minds and then translate)
//defines what happens for each passing thru. lil bit of a syntax trick. ++ equals plus 1. which means it adds 1 numebr every time it goes through the loop, until it reaches 20. remember the first and current value (val) will be set at 0
//++ could also be val + 1, or val ++,
// now we need another {} to define the scope of the code. anything thathappens here wont be accesbile outside this middle curly braces

Insert cell
Insert cell
Insert cell
{
let myArray = [0,1,2,3]
return myArray[2]
}

//class notes
Insert cell
{
let myArray = [0,1,2,3]
for (var i = 0; i <5; i++) {
console.log('accessing an array', myArray[i],i) //remember that output will show up in console
}
return myArray[i]
}
//can access elements in an array in the same way
//acessing every element in my aray through its index, thats being created on the fly in the for loop
Insert cell
{
let myArray = [0,1,2]
for (var whatever in myArray) { // same as this expression above: for (var i = 0; i <5; i++)
console.log(myArray[whatever],whatever)
}
}

//whatever is any variable that I give the name. usually this is i, but it couls be whatever
//will look through every element in teh array like above, with shorter syntax
//its going thru each index and then being used to access each element in the array
// right side is print index . left side is actually accessing element of the array
Insert cell
{
let array = [0, 1, 2, 3, 4]
for(var i in array){
array[i] = array[i] ** 2
}
return array;
}
Insert cell
Insert cell
{
var counter = 0
while(counter < 5){ //counter on outer loop is being modified
console.log(`The counter is ${counter}, which is less than 5.`)
counter += 1 //this loop inside modifies the counter <5 //
};
return counter
}
//printing the current element in the counter on the left side (see console)
// EB notes
Insert cell
//above. every time i go through a loop, I modify because my counter is getting updated
Insert cell
Insert cell
Insert cell
{
const num = 100
if(num == 100) {
return "100!"
}
else {
return "Not 100..."
}
}
Insert cell
{
let test = 25
if (test<50){
return 'this is a small number'
}
}

//we get the text printed because the conditional is tue (our test = 25 and thats less than 50)

//EB class notes
//conditionals work by setting up an if condition
Insert cell
{
let test = 55
if (test<50){
return 'this is a small number'
}
else {//add another expression
return 'this is a larger number'
}
}
//EB class notes
Insert cell
{
let test = 200

if (test<50){
return 'this is a small number'
}
else if (test > 100) { //else if can go above else but it requires a condition in the (parenthases) after it
return 'this is a very larger number'
}
else { //add another expression
return 'this is a large number'
}
}
//using else if, requires a condition
//note that we always put the return clause in within {} //
Insert cell
{
let test = 10

if (test<50 || test == 'whatever'){
return 'this is a small number'
}
else if (test > 100) { //else if can go above else but it requires a condition in the (parenthases) after it
return 'this is a very larger number'
}
else { //add another expression
return 'this is a large number'
}
}


//logical operators in a for loop
// we used || - make sure you get this EB
Insert cell
{
const num = 100
if(num == 99) {
return "99!"
}
else {
return "Not 99..."
}
}
Insert cell
Insert cell
{
var number = 100
if(number == 100){
return "Number is 100"
} else if(number < 100){
return "Number is less than 100."
} else if(number > 100){
return "Number is greater than 100."
}};
Insert cell
Insert cell
function multiply_here(number1,number2) {
return number1*number2
}

//EB class notes
//firstyou need a function name
//name you need to estab arguments/inputs called whatev we want
//
Insert cell
multiply_here(5,2)
//now can just call the function later and insert the actual input values
//EB class notes
Insert cell
function empty() {
return 'hello world'
}
//can create an empty function
//note for loops are not like this, variables "come up on the fly"
Insert cell
//can also specify values // this is part of pset 2 //v simple
Insert cell
hello = empty()
//call the empty function elsewhere
//EB notes
//"can use functions to actually return values." example is here.
Insert cell

// function declaration with parameters
function multiply_this(a,b) {
return a*b
}

Insert cell
{
// call the function, providing arguments for the parameters
const x = 4
const y = 5

return multiply_this(x,y) // run function with x and y as our arguments
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more