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
pushed 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
namesfilled with 5 names. - Write a
forloop 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 ifstatement where the condition ischoice1 === "paper". - Inside this
else ifstatement, write anif/elsestatement. 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);
Subscribe to:
Posts (Atom)