-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
50 lines (45 loc) · 1.9 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// *******************************
// START HERE IF YOU WANT A MORE CHALLENGING STARTING POINT FOR THIS ASSIGNMENT
// *******************************
//
// Module 4 Assignment Instructions.
//
// The idea of this assignment is to take an existing array of names
// and then output either Hello 'Name' or Good Bye 'Name' to the console.
// The program should say "Hello" to any name except names that start with a "J"
// or "j", otherwise, the program should say "Good Bye".
// STEP 1:
// Wrap the entire contents of script.js inside of an IIFE
// See Lecture 52, part 2
// (Note, Step 2 will be done in the SpeakHello.js file.)
( function () {
var names = ["Yaakov", "John", "Jen", "Jason", "Paul", "Frank", "Larry", "Paula", "Laura", "Jim"];
// STEP 10:
// Loop over the names array and say either 'Hello' or "Good Bye"
// using the 'speak' method or either helloSpeaker's or byeSpeaker's
// 'speak' method.
// See Lecture 50, part 1
for (var i = 0; i < names.length; i++) {
// STEP 11:
// Retrieve the first letter of the current name in the loop.
// Use the string object's 'charAt' function. Since we are looking for
// names that start with either upper case or lower case 'J'/'j', call
// string object's 'toLowerCase' method on the result so we can compare
// to lower case character 'j' afterwards.
// Look up these methods on Mozilla Developer Network web site if needed.
var firstLetter = names[i].charAt(0).toLowerCase();
// STEP 12:
// Compare the 'firstLetter' retrieved in STEP 11 to lower case
// 'j'. If the same, call byeSpeaker's 'speak' method with the current name
// in the loop. Otherwise, call helloSpeaker's 'speak' method with the current
// name in the loop.
if (firstLetter === 'j') {
// byeSpeaker.xxxx
byeSpeaker.speak(names[i]);
} else {
// helloSpeaker.xxxx
helloSpeaker.speak(names[i]);
}
}
}
)();