Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Last active August 29, 2015 14:24
Show Gist options
  • Save boddhisattva/fd953e1fe32a7fc83088 to your computer and use it in GitHub Desktop.
Save boddhisattva/fd953e1fe32a7fc83088 to your computer and use it in GitHub Desktop.
JS Learnings from codecademy course - http://www.codecademy.com/en/tracks/javascript
/* Abbreviations used :
L - Learning */
Question on hand -
/*
If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".
If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".
If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".
*/
// L1 - a
// Your implementation
if(computerChoice > 0 && computerChoice < 0.33) {
computerChoice = "rock"
}
else if(computerChoice > 0.34 && computerChoice < 0.66) {
computerChoice = "paper"
}
else {
computerChoice = "scissors"
}
// L2
// implementation from the exercise
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
// l3 - The complete game
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!"
}
else if(choice1 === "rock"){
if (choice2 === "scissors") {
return "rock wins"
}
else {
return "paper wins"
}
}
else if(choice1 === "paper"){
if (choice2 === "scissors") {
return "scissors wins"
}
else {
return "paper wins"
}
}
else if(choice1 === "scissors"){
if (choice2 === "paper") {
return "scissors wins"
}
else {
return "rock wins"
}
}
}
compare(userChoice, computerChoice);
// Learning 1 - usage of prompt
// Check if the user is ready to play!
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'")
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage")
// We use `===` for comparison
if(userAnswer === "yes")
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
else
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
confirm("I am ready to play!");
var age;
age = prompt("What's your age");
if (age < 13)
{console.log("allowed to play but you take no responsibility");}
else
{console.log("Let's get started!");}
var feedback = prompt("Kindly provide a feedback about the amge")
if( feedback > 8)
console.log("Thank you! We should race at the next concert!");
else
console.log("I'll keep practicing coding and racing.");
//L3 - Indentation wrt if statements -
if (speed < 80) {
// Use console.log() to print "Slow down"
}
else {
// Use console.log() to print "Drive safe"
}
numbers = []
for(var i =1;i<=20;i++){
numbers.push(i);
}
numbers_count = 20;
current_number = 0;
while(current_number<numbers_count){
current_number++;
if(current_number%3 === 0 && current_number%5 === 0){
console.log("FizzBuzz");
}
else if(current_number%3 === 0 ){
console.log("Fizz");
}
else if(current_number%5 === 0 ){
console.log("Buzz");
}
else
{
console.log(current_number);
}
}
// L1 - Learning with local and global scope
var my_number = 7; //this has global scope
var timesTwo = function(number) {
var my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};
timesTwo(7);
console.log("Outside the function my_number is: ");
console.log(my_number);
// L2
/*jshint multistr:true */
/* Please note you have to specify var in your initializtion of the variable used as part of the loop */
/* Also, please note that JS follows four space indentation */
var text = "mohnish some text mohnish";
var myName = "mohnish";
var hits= [];
for(var i = 0; i < text.length ; i++){
if (text[0] === 'm') {
for(var j = i; j < (i + myName.length); j++){
hits.push(text[j]);
}
}
}
if (hits.length > 0){
console.log(hits)
}
else{
console.log("Your name wasn\'t called");
}
//L1 Basic while loop inside a condition -
//Remember to make your condition true outside the loop!
condition=true;
var soloLoop = function(){
//Your code goes here!
while(condition ){
console.log("Looped once!")
condition=false;
}
};
soloLoop();
//l2 - for & while in one function
var soloLoop = function(){
//Your code goes here!
while(condition ){
console.log("Looped once!")
condition=false;
}
for(var i=0;i<4;i++){
console.log('hi');
}
};
soloLoop();
//L3 a do while loop -
var loopCondition = false;
do {
console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");
} while (loopCondition);
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment