console.log(`Iteration number ${i}`)// Remember that the output will show up in the Console.
}
returni
}
//for every element in my array, perform something
{
for(varval=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
returnval
}
//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
{
letmyArray=[0,1,2,3]
returnmyArray[2]
}
//class notes
{
letmyArray=[0,1,2,3]
for(vari=0;i<5;i++){
console.log('accessing an array',myArray[i],i)//remember that output will show up in console
}
returnmyArray[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
{
letmyArray=[0,1,2]
for(varwhateverinmyArray){// 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
{
letarray=[0,1,2,3,4]
for(variinarray){
array[i]=array[i]**2
}
returnarray;
}
{
varcounter=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 //
};
returncounter
}
//printing the current element in the counter on the left side (see console)
// EB notes
//above. every time i go through a loop, I modify because my counter is getting updated
{
constnum=100
if(num==100){
return"100!"
}
else{
return"Not 100..."
}
}
{
lettest=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
{
lettest=55
if(test<50){
return'this is a small number'
}
else{//add another expression
return'this is a larger number'
}
}
//EB class notes
{
lettest=200
if(test<50){
return'this is a small number'
}
elseif(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 {} //
{
lettest=10
if(test<50||test=='whatever'){
return'this is a small number'
}
elseif(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
{
constnum=100
if(num==99){
return"99!"
}
else{
return"Not 99..."
}
}
{
varnumber=100
if(number==100){
return"Number is 100"
}elseif(number<100){
return"Number is less than 100."
}elseif(number>100){
return"Number is greater than 100."
}};
functionmultiply_here(number1,number2){
returnnumber1*number2
}
//EB class notes
//firstyou need a function name
//name you need to estab arguments/inputs called whatev we want
//
multiply_here(5,2)
//now can just call the function later and insert the actual input values
//EB class notes
functionempty(){
return'hello world'
}
//can create an empty function
//note for loops are not like this, variables "come up on the fly"
//can also specify values // this is part of pset 2 //v simple
hello=empty()
//call the empty function elsewhere
//EB notes
//"can use functions to actually return values." example is here.
// function declaration with parameters
functionmultiply_this(a,b){
returna*b
}
{
// call the function, providing arguments for the parameters
constx=4
consty=5
returnmultiply_this(x,y)// run function with x and y as our arguments
}
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.