Skip to content

Instantly share code, notes, and snippets.

@rivaadara111
Created January 28, 2016 18:39
Show Gist options
  • Save rivaadara111/ed6583d0a30628ef5f41 to your computer and use it in GitHub Desktop.
Save rivaadara111/ed6583d0a30628ef5f41 to your computer and use it in GitHub Desktop.
javascript basics examples
// expressions----------------------->
var mathResult = 1*10 + (200 / 3);
// an expression uses operators to give you a return value
//statement
if(false === true){
console.log('WTF');
}
else{
console.log('universe is OK');
}
//switch statement-------------------------->
switch(mathResult){
case 100: //if math result = 100
console.log("math result is 100"); //write result is 100
break; //something we have to write to seperate cases
case 2:
console.log("math result is 200");
break;
default: //you should always set a default aka. a code that will run if none of cases are true
console.log("math is easy");
}
//switch statements will work with any value, not just math
//you'll only see break; in a switch statement
/*switch statements are just more readable than if/else statements sometimes; switch statements are always a if/else statement; just different stylistically*/
//Functions------------------------------>
//1. Function Declartion
function fullName(firstName, lastName){
return firstName + ' ' + lastName;
}
console.log(fullName('Riva','Ridley'));
function sayName(name){
console.log(name);
}
//2. Function expression
var getTotal = function(number1, number2){
return number1 + number2;
};
getTotal();
//console.log(getTotal(1,1)); will print 2....
//var addition = getTotal (100,100)
//console.log(fullName('Riva','Ridley')); makes the function log/print out the answer using the function above
// Thunk example
var global = 100;
var thunk = function(){
var number = 10;
var returnFunction = function(){
var inner = "i'm hidden";
console.log(number, global, inner);
}; //this is thunk
return returnFunction; //this is calling the function
};
//function is a way of isolating values from the global scope
//purpose of functions is to set up scopes without influencing the purpose
//closes values we want to protect from the global scope
////this function will log 10, 100, and im hidden
//we;re running the function where the getTotal will equal the total of number1 and number2 together
var newFunction = thunk(); //this is calling the thunk function, the above is just declaring it
newFunction();
console.log(thunk()); //this is returning the inner function of thunk (return function)
//FizzBuzz
for (var i = 1; i <= 100; i++) {
//this is a specific type of loop called a 'for loop'
//'for' is one way of setting up a loop.
//it is going through every number from 1-100, checking the if and else, and then incrementing by +1 and running the if else statment again until it reaches 100
if (i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");}
else if (i % 3 === 0){
console.log("Buzz");
} else if (i % 5 === 0){
console.log("Fizz");
} else {
console.log(i);
}
}
//for every
//3. Recurision (aka. Recursive Functions)
function fizzBuzzer(i){
console.log(loopNumber);
loopnumber--;
if(loopNumber !== 0){
fizzBuzzer(loopNumber);
} else; {
return; //were not returning any value, we're just finishing the function ('returning' from it)
}
}
fizzBuzzer(100);
//we are calling the function inside of itself (hence 'recursion') -- its going to loop forever.
//recursive is a function that calls itself.
//we have passed the loop number back to itself, and it's decrementing each time
//counts down from 100-0 ; so we dont need a for loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment