|
| 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