Lab work 7 - ENTERING THE SECOND REALM - JavaScript in Plain Language (2015)

JavaScript in Plain Language (2015)

PART II: ENTERING THE SECOND REALM

2.8 Lab work 7

Searching for the position of a string character

In this project we will have variable x assigned to "orange".
The idea is to search for character "g" and display its location on the screen.

1- Declare variable x and assign it the string "orange".
var x = "orange";

2- Fill in a for loop form,
starting with i = 0;
and the range of i < x.length;

· orange has 6 characters so x.length will be 6 but we should not use number 6; we should program generically as much as we can because most of the times we don’t know the length of the value to be worked on.

3- In the body of the for loop create an if() condition that probes for the existence of character "g". The Boolean expression should look like this:
if(x[i] === "g") {

· In other words: if the character at current position i is g...

4- If the condition is true, print the position number of "g" to the screen.

5- Close the if() condition and then close the loop.

6- Test your code. It should display the number 4 as a result.

Extra practice:

a) Replace "g" on the if() statement with another character not contained in "orange", and create an else statement to catch it with the following output:
"That character was not found!"

(See discussion and result on the next page).

(Answer) Searching for the position of a character

See my own solution(s) here:
forum | bit.ly/XScVak

1- Declare variable x and assign it the string "orange".
var x = "orange";

2- Fill in a for loop form
starting with i = 0;
and the range of i< x.length;
for(var i = 0; i < x.length; i++){

3- In the body of the for loop create an if() condition that probes for the existence of character "g".
if(x[i] === "g"){

4- If the condition is true, print the position number of "g" to the screen.
console.log(i);

5- Close the if() condition and then close the loop.
}
}

If something is not clear, please read the previous topic Looping through a string of characters.

The last output, console.log(i); displays the position number of the character we are looking for.

What is the purpose of this exercise?

There are many applications that use these concepts. Right now I am preparing you to understand the next topic which will be about array lists. By the time you get there you will not have to struggle with concepts such as these.

For practicing purposes please do the next exercise on your own.

Counting numbers and declaring their odd/even quality

1- Create a for loop that counts from 0 to 10

2- For each count display their odd/even quality like for example:
1 is an odd number, 2 is an even number, etc.

See my own solution here ( sample number 3):
odd/even | bit.ly/XScVak

Counting apples

1- Create a for loop that counts up to 3 apples. Zero is not allowed and the output must say 1 apple if it’s single apple, then 2 apples, and finally 3 apples.

Instead of using i as a counter variable I am going to use a variable named apples. Please feel free to use whatever you want. The goal for this exercise is to practice more for loop constructs and to think on how would we distinguish between a single output and a plural output.

See my own solution here on exercise number 4:
apples ) bit.ly/XScVak

END OF LAB