Shared Flashcard Set

Details

Java Syntax
Various syntax for how to do things in java
23
Computer Science
Graduate
11/15/2014

Additional Computer Science Flashcards

 


 

Cards

Term
If / Else statements
Definition
if (condition) {
//Do something
}
else { //Otherwise...
// Do something else!
}
Term
Function syntax
Definition
var something = function(name){
console.log("Hello, " + name);
};

something("Jonathan")
Term
For Loops syntax
Definition
for (start; end; increment){
//Do something!
}

Ex:
for (i = 100; i >= 1; i -= 5){
console.log(i)
Term
Arrays syntax
Definition
var something = [];

var junk = ["ball", "stick", 100, 2];
console.log(junk); will display the list

junk[0]; will display the first element in the array
Term
Loops and arrays
Definition
var names = ["John", "Cody", "Steve"]

for (i = 0; i < 3; i++){
console.log("I people who are called", names[i]);
]
Term
while loops syntax
Definition
while(condition){
//Do something!
//be sure to close/set a while loop to be untrue after a certain point
}

var understand = true;

while(understand === 1){
console.log("I'm learning while loops!");
understand = false;
}

console.log("I'm learning while loops!");
Term
do / while loop syntax
Definition
do {
//The thing to do at least once!

} while( /* some condition */ );

var loopCondition = false;

do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while(loopCondition);
Term
Random 0 or 1 generator
Definition
youHit to Math.floor(Math.random() * 2). This sets youHit to a random number that's either 0 (which JavaScript reads as false) or 1 (which JavaScript reads as true).
Term
Math.floor(Math.random() *5 + 1);
Definition
First we use Math.random() to create a random number from 0 up to 1. For example, 0.5

Then we multiply by 5 to make the random number from 0 up to 5. For example, 0.5 * 5 = 2.5

Next we use Math.floor() to round down to a whole number. For example, Math.floor( 2.5 ) = 2

Finally we add 1 to change the range from between 0 and 4 to between 1 and 5 (up to and including 5)
Term
+= operator
Definition
Takes the variable in front of += and add the variable after it together.
from:
totalDamage = totalDamage + damageThisRound

to:
totalDamage += damageThisRound
Term
Slaying the dargon
Definition
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0

while(slaying){
if(youHit){
console.log("You have struck the dargon ferociusly! Kee-Ya!!");
totalDamage += damageThisRound;
if(totalDamage >= 4){
console.log("This dargon has doth been slain!");
slaying = false;
}
else{
youHit = Math.floor(Math.random() * 2);
}
}
else{
console.log("The dargon evaded the attack!");
}
slaying = false;
};
Term
isNaN
Definition
Is Not a Number

isNaN("butts"); // => true
isNaN(42); // => false

Be careful: if you call isNaN on a string that looks like a number, like '42', JavaScript will try to help by automatically converting the string '42' to the number 42 and return false (since 42 is a number)
Term
switch
Definition
Allows you to preset a number of options (called cases), then check an expression to see if it matches any of them. If there's a match, the program will perform the action for the matching case; if there's no match, it can execute a default option.

var lunch = prompt("What do you want for lunch?","Type your lunch choice here");

switch(lunch){
case 'sandwich':
console.log("Sure thing! One sandwich, coming up.");
break;
default:
console.log("Huh! I'm not sure what " + lunch + " is. How does a sandwich sound?");
}
Term
switch syntax
Definition
switch (/*Some expression*/) {
case 'option1':
// Do something
break;
case 'option2':
// Do something else
break;
case 'option3':
// Do a third thing
break;
default:
// Do yet another thing
}
Term
variable with prompt
Definition
var variableName = prompt("Question to the user");
Term
.toUpperCase()
.toLowerCase()
Definition
converts all the user's answer to ALL CAPS or all lowercase before saving it in the variable.

var variableName= prompt("Question to the user").toUpperCase();
Term
.length
Definition
When you call .length on an array, it returns the number of elements the array has.

var array = ["butts", " farts", 69];
console.log(array.length)
answer is '3'
Term
Iterating over an array. Combining arrays with a for loop and displaying the array as a list
Definition
var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];
for(var i = 0; i console.log(languages[i]);
};
Term
Heterogeneous arrays
Definition
An array that containts integers and strings

var hetero = ["one", 2, "three"];
Term
Multidimensional arrays
Definition
An array that contains another array.

var multiArray = [["wow", "much wow"], [1, 853]];
Term
Jagged arrays
Definition
A variation on the multidimensional array where each array contains a different number of elements

var jagged = [["fifty five", 55, "shfifty fhive"], [1, 54], "much array"];
Term
Object (Literal Syntax)
Definition
var myObj = {
key1 = value,
key2 = value
};
Term
Object (constructor)
Definition
var myObj = new Object();
myObj.key1 = value;
OR
myObj['key2'] = value;
Supporting users have an ad free experience!