Monday, March 31, 2014

logical operators

&&
and - true only if all are true, false if only one is false:

true && true // = true
true && false // = false
false && true // = false
false && false // = false
||
or - true if only one is true, false only if all are false:

true || true // = true
true || false // = true
false || true // = true
false || false // = false
!
not so !true = false and !false = true

Tuesday, March 25, 2014

switch

  • switch appears to be a form of if else statement except it is a loop of sorts
  • case is like if/else if of the loop, it checks to see if the condition is equal to the variable and then moves to the next case, unless their is a break
  • default is always run unless the program breaks, functions like the else
var answer = prompt("What do you do in your free time?"); switch(answer) { case 'play games': console.log("you like me and probably have no life"); break; // Add your code here! case 'program': console.log("you are like me and have no life, but you at least have a skill for a job"); break; case 'skate': console.log("seems like you get around"); break; case 'hang out with friends': console.log("you are lucky to have so many"); break; default: console.log("well that's interesting"); }

Thursday, March 20, 2014

Wednesday, March 12, 2014

dragon slayer

var slaying=true; var youHit=Math.floor(Math.random()*2); var damageThisRound=Math.floor(Math.random()*5 + 1); var totalDamage=totalDamage+damageThisRound; while(slaying) { if (youHit===1) { console.log("you hit the dragon"); if (totalDamage>=4) { console.log("you slew the dragon!"); slaying=false; } else { youhit=Math.floor(Math.random()*2); } } else { console.log("the dragon burnates you, you are dead"); } slaying=false; } play this game on my site

Monday, March 10, 2014

while loops

While loops continue to run as long as there condition is true

do is used to run something at least once and then as long as the following while loop is true

Friday, March 7, 2014

for loops and arrays

I finally found out the problem with my js. One must be sure to have the correct String being pushed

var text=":LJSDFkl as;jdfk Jonathan LKSJFDkjasfjioodsj kdjfoa[jsdif oajdfioasdjfjkads ioah fidha oiioreah iodsajfoi;dhadoi g Jonathan ] alksjdfkasjdfiojadsiojfioajsdifojsdaiojfioajsdofijaoispjf Jonathan"; var myName = "Jonathan"; var hits = []; for (var i = 0; i < text.length; i++) { if (text[i] === myName[0]) { for (j = i; j <= i + myName.length; j++) { hits.push(text[j]); } } } console.log(hits);

Arrays

arrays are sets of data, both strings and numbers can be in an array at the same time, arrays are stored in variables

var myarray[4, "annoying", "food" 5]

Thursday, March 6, 2014

For loops

for loops take a variable and do something whenever that variable is within a certain range, they also always have a loop counter which makes me think that while loops came first

for (i=1; i<=10; i++) { console.log(i); }