Skip to content

Commit 99a47ad

Browse files
committed
assigment4
1 parent 62fab0a commit 99a47ad

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// NOTE! The steps in this file are basically identical to the ones you
2+
// performed in the SpeakHello.js file.
3+
4+
// STEP 6: Wrap the entire contents of SpeakGoodBye.js inside of an IIFE
5+
// See Lecture 52, part 2
6+
7+
8+
// STEP 7: Create an object, called 'byeSpeaker' to which you will attach
9+
// the "speak" method and which you will expose to the global context
10+
// See Lecture 52, part 1
11+
// var byeSpeaker =
12+
13+
// DO NOT attach the speakWord variable to the 'byeSpeaker' object.
14+
var speakWord = "Good Bye";
15+
16+
// STEP 8: Rewrite the 'speak' function such that it is attached to the
17+
// byeSpeaker object instead of being a standalone function.
18+
// See Lecture 52, part 2
19+
function speak(name) {
20+
console.log(speakWord + " " + name);
21+
}
22+
23+
// STEP 9: Expose the 'byeSpeaker' object to the global scope. Name it
24+
// 'byeSpeaker' on the global scope as well.
25+
// xxxx.xxxx = byeSpeaker;
26+
(function(window){
27+
var speakWord = "Good Bye";
28+
var byeSpeaker = {};
29+
byeSpeaker.sayGoodBye = function speak(name) {
30+
console.log(speakWord + " " + name);
31+
}
32+
window.byeSpeaker = byeSpeaker;
33+
})(window);

module4-solution/harder/SpeakHello.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// STEP 2: Wrap the entire contents of SpeakHello.js inside of an IIFE
2+
// See Lecture 52, part 2
3+
4+
5+
// STEP 3: Create an object, called 'helloSpeaker' to which you will attach
6+
// the "speak" method and which you will expose to the global context
7+
// See Lecture 52, part 1
8+
// var helloSpeaker =
9+
10+
// DO NOT attach the speakWord variable to the 'helloSpeaker' object.
11+
var speakWord = "Hello";
12+
13+
// STEP 4: Rewrite the 'speak' function such that it is attached to the
14+
// helloSpeaker object instead of being a standalone function.
15+
// See Lecture 52, part 2
16+
function speak(name) {
17+
console.log(speakWord + " " + name);
18+
}
19+
20+
// STEP 5: Expose the 'helloSpeaker' object to the global scope. Name it
21+
// 'helloSpeaker' on the global scope as well.
22+
// See Lecture 52, part 2
23+
// (Note, Step 6 will be done in the SpeakGoodBye.js file.)
24+
// xxxx.xxxx = helloSpeaker;
25+
26+
(function(window){
27+
var speakWord = "Hello";
28+
var helloSpeaker = {};
29+
helloSpeaker.sayHello = function speak(name){//(helloSpeaker.name) {
30+
console.log(speakWord + " " + name);//helloSpeaker.name);
31+
}
32+
window.helloSpeaker = helloSpeaker;
33+
})(window);

module4-solution/harder/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Module 4 Solution Starter</title>
6+
<script>
7+
var names = []; // DO NOT REMOVE
8+
</script>
9+
<script src="SpeakHello.js"></script>
10+
<script src="SpeakGoodBye.js"></script>
11+
<script src="script.js"></script>
12+
</head>
13+
<body>
14+
<h1>Module 4 Solution Starter</h1>
15+
</body>
16+
</html>

module4-solution/harder/script.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// *******************************
2+
// START HERE IF YOU WANT A MORE CHALLENGING STARTING POINT FOR THIS ASSIGNMENT
3+
// *******************************
4+
//
5+
// Module 4 Assignment Instructions.
6+
//
7+
// The idea of this assignment is to take an existing array of names
8+
// and then output either Hello 'Name' or Good Bye 'Name' to the console.
9+
// The program should say "Hello" to any name except names that start with a "J"
10+
// or "j", otherwise, the program should say "Good Bye". So, the final output
11+
// on the console should look like this:
12+
/*
13+
Hello Yaakov
14+
Good Bye John
15+
Good Bye Jen
16+
Good Bye Jason
17+
Hello Paul
18+
Hello Frank
19+
Hello Larry
20+
Hello Paula
21+
Hello Laura
22+
Good Bye Jim
23+
24+
WARNING!!! WARNING!!!
25+
The code does NOT currently work! It is YOUR job to make it work
26+
as described in the requirements and the steps in order to complete this
27+
assignment.
28+
WARNING!!! WARNING!!!
29+
30+
*/
31+
32+
// STEP 1:
33+
// Wrap the entire contents of script.js inside of an IIFE
34+
// See Lecture 52, part 2
35+
// (Note, Step 2 will be done in the SpeakHello.js file.)
36+
37+
var names = ["Yaakov", "John", "Jen", "Jason", "Paul", "Frank", "Larry", "Paula", "Laura", "Jim"];
38+
39+
// STEP 10:
40+
// Loop over the names array and say either 'Hello' or "Good Bye"
41+
// using the 'speak' method or either helloSpeaker's or byeSpeaker's
42+
// 'speak' method.
43+
// See Lecture 50, part 1
44+
for (var i = 0; i < names.length; i++) {
45+
46+
// STEP 11:
47+
// Retrieve the first letter of the current name in the loop.
48+
// Use the string object's 'charAt' function. Since we are looking for
49+
// names that start with either upper case or lower case 'J'/'j', call
50+
// string object's 'toLowerCase' method on the result so we can compare
51+
// to lower case character 'j' afterwards.
52+
// Look up these methods on Mozilla Developer Network web site if needed.
53+
// var firstLetter =
54+
55+
var firstLetter = names[i].charAt(0);
56+
// console.log("La primera letra es: " + firstLetter);
57+
// console.log("La primera letra es: " + firstLetter.toLocaleLowerCase());
58+
59+
// STEP 12:
60+
// Compare the 'firstLetter' retrieved in STEP 11 to lower case
61+
// 'j'. If the same, call byeSpeaker's 'speak' method with the current name
62+
// in the loop. Otherwise, call helloSpeaker's 'speak' method with the current
63+
// name in the loop.
64+
if ('j' === firstLetter.toLowerCase()) {
65+
byeSpeaker.sayGoodBye(names[i]);
66+
// console.log("bye");
67+
} else {
68+
helloSpeaker.sayHello(names[i]);
69+
// console.log("hello");
70+
}
71+
}

0 commit comments

Comments
 (0)