Tuesday, 15 July 2014

== Search Text for Your Name tutorial 5/7 ==
Okay! Last loopy step: add another 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 like
for(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 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:
  1. Create an array called names filled with 5 names.
  2. Write a for loop that prints "I know someone called " followed by names[i]. Make sure there's a space between "called" and the name!
  3. Run your code and the five sentences should print out.
Solve:

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

== Array positions ==

Instructions:
Print out the fourth element of the array.
1. Start with figuring out how to express what the fourth element in the array is.
2. Then use console.log() to print things out!

Solve:
var junkData = ["Eddie Murphy", 49, "peanuts", 31];
console.log(junkData[3]);