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

JavaScript in Plain Language (2015)

PART II: ENTERING THE SECOND REALM

2.12 Lab work 9

NOTE: If you want to do this exercise along with me jump the next 14 steps.
Reserve this page for future practice.

1- Take the following array y as an example and sort the array:
var y = ["grape", "apple", "banana"];

2- Now assign to a new array mySort, the contents of the sorted array y.

3- Test mySort by displaying its contents on the Console.

4- Now, display the array mySort in a string type format and
split the items with 1 blank space. Hint: use join().

5- Now, instead of just displaying it, assign the items from mySort to a new variable z, but in a string format.
Use a combination of space, forward slash, and space as a separator.

6- Call z.

7- Use the indexOf() method to find the position of "banana" in array y.

8- Test for the existence of "banana" in array y ( use an if conditional statement), and display the following message on the screen using console.log():
"banana is contained in array y"

9- Using the same method as the last exercise, look for "chestnut", then
display two messages, one for if() and another for else as follows:
"chestnut is contained in array y"
else: "chestnut is not found in array y".

10- Remove the last item of array mySort using pop():

11- Remove the first item of array mySort using shift():

12- The array mySort should now only contain "banana".
Add "chestnut" as the last item.
Use push().

mySort should now have 2 items, ["banana", "chestnut"]

13- Using splice(), insert "apple" in array mySort at position 1 (as a second item). Do not delete any existing item.

mySort should now contain ["banana", "apple", "chestnut"]

14- Using slice(), create a new array named newArray with a copy of the second and third items ( "apple" and "chestnut") from mySort array.
The newArray should now contain ["apple", "chestnut"].

(See my results on the next page.)

Answers to Lab work 9:

1- Take the following array y as an example and sort the array:
var y = ["grape", "apple", "banana"];
y.sort();

It returns ["apple", "banana", "grape"]

2- Assign a new array named mySort to the contents of a sorted array y:
var mySort = y.sort();

3- Test mySort:
mySort;

It returns ["apple", "banana", "grape"]

4- Now display the array mySort is a string type format,
and split the items whit 1 blank space. Hint: use join():
mySort.join(" ");
or you might have done this which is also correct:
console.log(mySort.join(" "));

It returns "apple banana grape" in a string format.

5- Now, instead of just displaying it, assign the items from mySort to a new variable z, but in a string format. Use a combination of space, forward slash, and space as a separator:
var z = mySort.join(" / ");

6- Call z:
z;

It returns "apple / banana / grape"

7- Use the indexOf() method to find the position of "banana" in array y:
y.indexOf("banana");

It returns 1, which means that "banana" exists and is located on position1 (second item).

8- Test for the existence of "banana" in array y ( use an if conditional statement), and display the following message on the screen using console.log():
"banana is contained in array y":
if(y.indexOf("banana") >=0){
console.log("banana is contained in array y");
}

It returns "banana is contained in array y"

9- Using the same method as the last exercise, look for "chestnut".
Then display two messages, one for if() and another for else as follows:
"chestnut is contained in array y"
else: "chestnut is not found in array y":
if(y.indexOf("chestnut") >=0){
console.log("chestnut is contained in array y");
} else {
console.log("chestnut is not found in array y");
}

It returns "chestnut is not found in array y"

10- Remove the last item of array mySort using pop():
mySort.pop();

It removes "grape"

11- Remove the first item of array mySort using shift():
mySort.shift();

It removed "apple"

12- mySort should now only contain "banana".
Add "chestnut" as the last item, using push():
mySort.push("chestnut");

mySort should now have 2 items, ["banana", "chestnut"]

13- Using splice(), insert "apple" in array mySort at position 1 (as a second item) and
do not delete any existing item.
mySort.splice(1,0,"apple");

mySort should now contain ["banana", "apple", "chestnut"]

14- Using slice(), create a new array named newArray with a copy of the second and third items ( "apple" and "chestnut") from mySort array:
var newArray = mySort.slice(1,3);

The newArray now contains ["apple", "chestnut"].
The argument 1 represents the second location from mySort, and argument 3 represents the exclusion which starts at location 3.

· Yes, location 3 does not exist, but we had to cover location 2 in order to include chestnut. The exclusion started at location3, which is item 4.

It may be overwhelming at first.

Practice, practice, practice.

END OF LAB