Skip to content

Instantly share code, notes, and snippets.

@gurlock
Created August 17, 2017 16:11
Show Gist options
  • Save gurlock/4e337d4bd62164d9ecdb1d038f20540c to your computer and use it in GitHub Desktop.
Save gurlock/4e337d4bd62164d9ecdb1d038f20540c to your computer and use it in GitHub Desktop.
Basic JS notes

SSP Day N/A

Javascript Basics

Four Basic Values

  • As the size of the problem gets bigger, the cost might grow quickly/slowly/not at all.
  • Mathematical comparison between different plans, or algorithm.
  • Cheat sheet- bigocheatsheet.com working

Four Basic Values Numbers 13 Operators - arthimetic symbols

% - remainder or MODULO, 314%100=14 Strings “Frankly, my dear, I don’t give a damn” \n -new line!! Enter Can add strings “con” + “cat” Booleans Distinguishes between two possibilities, true and false, like on and off ! flips value !true =false Objects Functions Undefined Values Operators Variables Keyword var defines a variable After defined, can be used as an expression var ten = 10; console.log(ten * ten); // → 100 Can;t use variables break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield Functions Values given to functions are called arguments Alert (“Hallo”); Hallo is argument Console.log = output values in JS console Pop-up JS dialogs Prompt - tell me everything Confirm - pop up no open text box Conditional Exeuction If (rock{ console.log(“you win”); }{Else Else if Unless the number is not a number , aka !isNaN var num = Number(prompt("Pick a number", "0"));

if (num < 10) alert("Small"); else if (num < 100) alert("Medium"); else alert("Large"); While/Do - occurs with loops, aka printing all the numbers until it hits 12 While, loops through Do, DOES SOMETHING, then loops FOR = shortened version of Do while. For (var number=0; number <=12; number=number +2) console.log(number0; For the number zero, as long as the results are under 12, add 2 to the number, and keep printing the number until it hits 12. First part - initiliazes loop Second part - checks if loop should continue Last part- updates state of loop after eery attempt Var result=1; For (var counter=0; counter <10; counter=counter+1) result=result2; Update variable succintly Number =number +1 Counter-counter+1 counter+=1 For (var number=0; number<=12; number+=2) conole.log(number); Counter++ = up by 1 Counter-- = down by one result= double result Rock paper scissors game if (variable == "value1") action1(); else if (variable == "value2") action2(); else if (variable == "value3") action3(); else defaultAction(); Could also say switch (prompt("What is the weather like?")) { case "rainy": console.log("Remember to bring an umbrella."); break; case "sunny": console.log("Dress lightly."); case "cloudy": console.log("Go outside."); break; default: console.log("Unknown weather type!"); break; } CAPITALIZE VERY WORD BUT FIRST /* / Programs are built of statements, which sometimes contain more statements, and are usually built of expressions. Programs execute statements from top to bottom. You an use conditional (If, else, switch ) or looping (while, do for ) statements to make it not flow like a waterfall Var count Var poundSign=”#” For (var poundSign=”#”; line.length<=7; poundSign+=”#”) conole.log(poundSign); Chapter 3 : Functions Var square = function (x) { returnxx; }; We define a variable as a certain operation, like saying “square” means computer find the square root Var square=function (x) IS THE SAME AS function square(x){ returnx*x} STRING can be letters together or numbers together SUMMARY FUNCTIONS Function is a keyword, that can create a value when used as an expression When used as a statement, can decalre a variable and give a function to its calue // Create a function value f var f = function(a) { console.log(a + 2); };

// Declare g to be a function function g(a, b) { return a * b * 3.5; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment