Skip to content

Instantly share code, notes, and snippets.

@cipto-hd
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cipto-hd/980eac2fb13b650aecc3 to your computer and use it in GitHub Desktop.
Save cipto-hd/980eac2fb13b650aecc3 to your computer and use it in GitHub Desktop.
Javascript course at Code Academy
var slaying = true;
var youHit;
var damageThisRound;
var totalDamage =0;
var totalHit =0;
while(slaying){
youHit = Math.floor(Math.random() * 2);
if(youHit){
totalHit++;
console.log("You hit the dragon!");
damageThisRound = Math.floor(Math.random()*5 + 1);
totalDamage += damageThisRound;
if(totalDamage>=4) {
console.log("You won!");
slaying = false;
}
}else{
console.log("You're defeated!");
slaying = false;
}
if(!slaying){
console.log("totalHit: " + totalHit);
console.log("totalDamage: " + totalDamage);
}
}
// Write your code below!
for(var i=0;i<3;i++){console.log("Subhanallah")}
var i=3;
while(i--){console.log("Alhamdulillah")}
var i=2;
do{console.log("La ilaha illallah")}while(i--)
/*
Find a name on strings using array, for loops, substr
*/
text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
var myName = "blah";
var hits = [];
// Look for first letter of the name in the text
for(var i = 0; i < text.length; i++) {
if (text[i] == myName[0]) {
// If we find it, add characters up to
// the length of my name to the array
var foundName = text.substr(i,myName.length);
if(foundName===myName)hits.push(foundName);
i+=myName.length;
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
/*
Rock, Paper, Scissors choice game, Code Academy course js exercise
*/
var userChoice;
var getUserChoice = function(){
var userChoice = prompt("Choose rock, paper, scissors!");
if((userChoice != null) && !(userChoice==="rock" || userChoice==="paper" || userChoice==="scissors" )) {getUserChoice();}
return userChoice;
};
userChoice = getUserChoice();
if(userChoice === null) console.log("Cancel play the game");
else {
var computerChoice = Math.random(); userChoice
if (computerChoice < 0.34) { computerChoice = "rock"; }
else if(computerChoice <= 0.67) { computerChoice = "paper"; }
else { computerChoice = "scissors"; }
console.log("Computer: " + computerChoice);
console.log("You: " + userChoice);
var compare = function(choice1,choice2){
if(choice1 === choice2) return "tie";
else if(choice1==="rock"){
if(choice2==="scissors") return "rock";
else return "paper";
}else if(choice1==="paper"){
if(choice2==="rock") return "paper";
else return "scissors"; }
else{
if(choice2==="rock") return "rock";
else return "scissors"; }
}
var result = compare(userChoice,computerChoice);
if(result=="tie") message = "The result is tie!"
else{
if(result==userChoice) winner = "You";
else winner = "Computer"
message = "The winner is " + winner;
}
console.log(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment