Tuesday, 5 August 2014
Tuesday, 15 July 2014
== Search Text for Your Name tutorial 5/7 ==
Okay! Last loopy step: add another
This loop will make sure each character of your name gets
For your second
First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with
Finally, in the body of your loop, have your program use the
for
loop, this time inside the body of your if
statement (between the if
's {}
s).This loop will make sure each character of your name gets
push
ed to the final array. The if
statement says: "If we find the first letter of the name, start the second for
loop!" This loop says: "I'm going to add characters to the array until I
hit the length of the user's name." So if your name is 11 letters long,
your loop should add 11 characters to hits
if it ever sees the first letter of myName
in text
.For your second
for
loop, keep the following in mind:First, you'll want to set your second loop's iterator to start at the first one, so it picks up where that one left off. If your first loop starts with
for(var i = 0; // rest of loop setup
your second should be something likefor(var j = i; // rest of loop setup
Second, think hard about when your loop should stop. Check the Hint if you get stuck!Finally, in the body of your loop, have your program use the
.push()
method of hits
. Just like strings and arrays have a .length
method, arrays have a .push()
method that adds the thing between parentheses to the end of the array. For example,newArray = [];
newArray.push('hello');
newArray[0]; // equals 'hello'
Solve:
var text = "Hello world Keya how you doing Keya";
var myName ="Keya";
var hits = [];
for(var i=0; i<text.length; i++ )
{
if (text[i]=== 'K')
{
for (var j=i; j< i+ myName.length; j++)
{
hits.push(text[j]);
}
}
}
output: 8
Search Text for Your Name Tutorial 3#
Instructions:
Below your existing code, create a
Solve
var text = "Hello world Keya how you doing Keya";
var myName ="Keya";
var hits = [ ];
for(var i=0; i<text.length; i++ )
{}
Instructions:
Below your existing code, create a
for
loop that starts at 0, continues until it reaches the end of text
,
and increments by 1 each time. (This means it will check each character
in the string.) There's no need to write anything between the {}
s of your loop just yet.Solve
var text = "Hello world Keya how you doing Keya";
var myName ="Keya";
var hits = [ ];
for(var i=0; i<text.length; i++ )
{}
Sunday, 13 July 2014
Loops and arrays II
Instructions:
var name = ["a","b","c","d","e"];
for(var i=0; i<5; i++)
{
console.log ("I know someone called "+name[i]);
}
Output:
I know someone called a
I know someone called b
I know someone called c
I know someone called d
I know someone called e
Instructions:
- Create an array called
names
filled with 5 names. - Write a
for
loop that prints"I know someone called "
followed bynames[i]
. Make sure there's a space between"called"
and the name! - Run your code and the five sentences should print out.
var name = ["a","b","c","d","e"];
for(var i=0; i<5; i++)
{
console.log ("I know someone called "+name[i]);
}
Output:
I know someone called a
I know someone called b
I know someone called c
I know someone called d
I know someone called e
Sunday, 6 July 2014
Saturday, 28 June 2014
== Build "Rock, Paper, Scissors" ==
Tutorial 7#
Tutorial 7#
What if choice1 is paper?
Now what if
a. if
b. if
Instructions:
/*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);
choice1
is "paper"
? Given choice1 is "paper"
,a. if
choice2 === "rock"
, then "paper"
wins.b. if
choice2 === "scissors"
, then "scissors"
wins.Instructions:
- Inside the
compare()
function under the existing code, write anotherelse if
statement where the condition ischoice1 === "paper"
. - Inside this
else if
statement, write anif
/else
statement. Ifchoice2 === "rock"
, return"paper wins"
. Else, return"scissors wins"
.
/*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#
You're doing great! Now we consider the other scenarios. Let's break the problem down a little. What if
a. if
b. if
Problem:
Let's code our outline from above:
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);
Tutorial 6#
What if choice1 is rock?
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:
- Inside the
compare()
function under the existing code, write anelse if
statement where the condition ischoice1 === "rock"
. - Inside this
else if
statement, write anif
/else
statement. Ifchoice2 === "scissors"
, return"rock wins"
. Else, return"paper wins"
.
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
Problem:
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";
}
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:
- If
computerChoice
is between 0 and 0.33, makecomputerChoice
equal to"rock"
. - If
computerChoice
is between 0.34 and 0.66, makecomputerChoice
equal to"paper"
. - If
computerChoice
is between 0.67 and 1, makecomputerChoice
equal to"scissors"
.
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
var computerChoice= Math.random();
console.log (computerChoice);
output:
0.92242
Tutorial 3#
Instructions
- Under your previous code, declare a variable called
computerChoice
and make it equal toMath.random()
. - Print out
computerChoice
so you can see howMath.random()
works. This step isn't needed for the game - just useful for learning!
var computerChoice= Math.random();
console.log (computerChoice);
output:
0.92242
Thursday, 26 June 2014
Tutorial 12#
== Functions & if / else ==
== Functions & if / else ==
An especially useful application of reusable code is
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
Instructions:
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!");
}
};
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:
- Write a function named
sleepCheck
that takes the parameternumHours
- Inside the function, write an
if
statement where if the number of hours of sleep is greater than or equal to 8, the computer willreturn
"You're getting plenty of sleep! Maybe even too much!";
. - Otherwise (
else
) if the number of hours of sleep is less than 8, have the computerreturn
"Get some more shut eye!";
- Call the function with 10 hours of sleep, like this:
sleepCheck(10);
- Call the function with 5 hours of sleep.
- Call the function with 8 hours of sleep.
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 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
== 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 scopevar 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
Saturday, 1 February 2014
Hello World,
Start the first day of blogging, with new hope, new Dream. So at starting i would like to introduce My self. I am Keya, Have lots of hopes for my country and my people :) always optimistic. Passed my school and college from Khulna(The most wonderful city In the world) now leaving in Dhaka.
In this world there are lots of things which can help us to organize our life and a great future for the followers, But one thing that is most important and day by day its changing that is the attitude and helping tendency of the people. Now a days the exchange of information became a practice among us. So I decided what ever I learn i will share my views for the followers. Only then we can change our country. Leave the politicians with there situation. Take a step forward to motivate the young talent. Each and every nation need a support system or you can say the back up, I will play my duty..........
So for the first step I have started to learn JAVA. and I will keep update my information... :) Hope it will help a person who is just at the starting point like me.
So nothing is impossible ~KN 02.01.2014
Start the first day of blogging, with new hope, new Dream. So at starting i would like to introduce My self. I am Keya, Have lots of hopes for my country and my people :) always optimistic. Passed my school and college from Khulna(The most wonderful city In the world) now leaving in Dhaka.
In this world there are lots of things which can help us to organize our life and a great future for the followers, But one thing that is most important and day by day its changing that is the attitude and helping tendency of the people. Now a days the exchange of information became a practice among us. So I decided what ever I learn i will share my views for the followers. Only then we can change our country. Leave the politicians with there situation. Take a step forward to motivate the young talent. Each and every nation need a support system or you can say the back up, I will play my duty..........
So for the first step I have started to learn JAVA. and I will keep update my information... :) Hope it will help a person who is just at the starting point like me.
So nothing is impossible ~KN 02.01.2014
Subscribe to:
Posts (Atom)