Saturday, 28 June 2014

== Build "Rock, Paper, Scissors" ==
Tutorial 7#
What if choice1 is paper?
Now what if choice1 is "paper"? Given choice1 is "paper",
a. if choice2 === "rock", then "paper" wins.
b. if choice2 === "scissors", then "scissors" wins.

Instructions:
  1. Inside the compare() function under the existing code, write another else if statement where the condition is choice1 === "paper".
  2. Inside this else if statement, write an if / else statement. If choice2 === "rock", return "paper wins". Else, return "scissors wins".
Solve:
/*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 if(choice1=== "paper")
            {if(choice2 === "rock")
            {
                return "paper wins";
                }else return "scissors wins";
                }
           
            else return "paper wins";
            }
        }
};
compare("rock",1);

== Build "Rock, Paper, Scissors" ==
Tutorial 6#
What if choice1 is rock?
You're doing great! Now we consider the other scenarios. Let's break the problem down a little. What if choice1 is "rock"? Given choice1 is "rock",
a. if choice2 === "scissors", then "rock" wins.
b. if choice2 === "paper", then "paper" wins.

Problem:
Let's code our outline from above:
  1. Inside the compare() function under the existing code, write an else if statement where the condition is choice1 === "rock".
  2. Inside this else if statement, write an if / else statement. If choice2 === "scissors", return "rock wins". Else, return "paper wins".
Solve:
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";
            }
        }
};
compare("rock",1);
== Build "Rock, Paper, Scissors" ==
Computer Choice: Part 2
We have computerChoice but it now equals a random number between 0 and 1. We need to somehow translate this random number into a random choice of rock, paper, or scissors. How do we do this?!
 Problem:
  1. If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".
  2. If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".
  3. If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".
Solution:
var computerChoice= Math.random();
if(0.33<computerChoice < 0)
{
 computerChoice= "rock";
}
else if(0.64<computerChoice < 0.34)
{
 computerChoice= "paper";
}else(0.67<computerChoice < 1)
{
 computerChoice= "scissors";
}

Friday, 27 June 2014

== Build "Rock, Paper, Scissors" ==
Tutorial 3#
Instructions
  1. Under your previous code, declare a variable called computerChoice and make it equal to Math.random().
  2. Print out computerChoice so you can see how Math.random() works. This step isn't needed for the game - just useful for learning!
Code:
var computerChoice= Math.random();
console.log (computerChoice);
output:
 0.92242
=== Build "Rock, Paper, Scissors ===
Tutorial 2#
User Choice:
Instructions:
  1. Declare a variable called userChoice.
  2. Make the variable equal to the answer we get by asking the user "Do you choose rock, paper or scissors?"
Code:
var userChoice = prompt("Do you choose rock, paper or scissors?");

Thursday, 26 June 2014

Tutorial 12#
== Functions & if / else ==

An especially useful application of reusable code is if/else statements. These can be very wordy, and a pain to type repeatedly.
We are going to write a function that checks how many hours of sleep a night you're getting. Inside the function will be an if/else statement. We want the function to check many different numbers of hours to see whether a person is getting enough sleep.

Instructions:
  1. Write a function named sleepCheck that takes the parameter numHours
  2. Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";.
  3. Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!";
Then call the function with different hours of sleep
  1. Call the function with 10 hours of sleep, like this: sleepCheck(10);
  2. Call the function with 5 hours of sleep.
  3. Call the function with 8 hours of sleep.
Solve:
var sleepCheck= function(numHours)
{
     sleepCheck(8);
    if(numHours >= 8)
    {
        return console.log("You're getting plenty of sleep! Maybe even too much!");
        }
        else {
            return console.log("Get some more shut eye!");
            }
    }; 

codecademy: Introduction to Functions in JS Tutorial 10#
== Global and local Variable in JS ==
Let's talk about an important concept: scope. Scope can be global or local.

Variables defined outside a function are accessible anywhere once they have been declared. They are called global variables and their scope is global.
For example:
var globalVar = "hello";

var foo = function() {
    console.log(globalVar);  // prints "hello"
};
 
// The variable globalVar can be accessed anywhere, 
even inside the function foo.
 
Variables defined inside a function are local variables
They cannot be accessed outside of that function.
var bar = function() {
    var localVar = "howdy";
}

console.log(localVar);  // error 
 
The var keyword creates a new variable in the current scope
That means if var is used outside a function, that variable has a global scope. 
If var is used inside a function, that variable has a local 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);
 Out put:
Inside the function my_number is 14
Outside the function my_number is 7