From b6d776f4d572cde79ccc1b3691bb0a6b88077a74 Mon Sep 17 00:00:00 2001 From: psreyes Date: Mon, 11 Jun 2018 13:18:01 +0200 Subject: [PATCH 1/2] Update README.md From 353186154ef75f48037b1f758115b6535334be2b Mon Sep 17 00:00:00 2001 From: Pablo Date: Mon, 11 Jun 2018 17:34:00 +0200 Subject: [PATCH 2/2] looping con javascript --- assignment4-solution-starter/README-FIRST.txt | 18 +++++ .../easier/SpeakGoodBye.js | 25 +++++++ .../easier/SpeakHello.js | 24 +++++++ .../easier/index.html | 13 ++++ assignment4-solution-starter/easier/script.js | 68 ++++++++++++++++++ .../harder/SpeakGoodBye.js | 31 ++++++++ .../harder/SpeakHello.js | 32 +++++++++ .../harder/index.html | 16 +++++ assignment4-solution-starter/harder/script.js | 72 +++++++++++++++++++ 9 files changed, 299 insertions(+) create mode 100644 assignment4-solution-starter/README-FIRST.txt create mode 100644 assignment4-solution-starter/easier/SpeakGoodBye.js create mode 100644 assignment4-solution-starter/easier/SpeakHello.js create mode 100644 assignment4-solution-starter/easier/index.html create mode 100644 assignment4-solution-starter/easier/script.js create mode 100644 assignment4-solution-starter/harder/SpeakGoodBye.js create mode 100644 assignment4-solution-starter/harder/SpeakHello.js create mode 100644 assignment4-solution-starter/harder/index.html create mode 100644 assignment4-solution-starter/harder/script.js diff --git a/assignment4-solution-starter/README-FIRST.txt b/assignment4-solution-starter/README-FIRST.txt new file mode 100644 index 0000000..6c24f6a --- /dev/null +++ b/assignment4-solution-starter/README-FIRST.txt @@ -0,0 +1,18 @@ +YOU HAVE A CHOICE! + +If you want a slightly more challenging assignment, use the code in the “harder” +folder as your starting point. If you want a slightly less challenging +assignment, use the code in the “easier” folder as your starting point. + +The difference between the two starting points is that in the “easier” +starting point, there are a few steps that are already completed for you. + +Harder: +If you want a slightly more challenging assignment, copy all the contents of +the ‘harder’ folder into your newly created solutions container folder for +this assignment, e.g., ‘module4-solution’. + +Easier: +If you want a slightly less challenging assignment, copy all the contents of +the ‘easier’ folder into your newly created solutions container folder for +this assignment, e.g., ‘module4-solution’. diff --git a/assignment4-solution-starter/easier/SpeakGoodBye.js b/assignment4-solution-starter/easier/SpeakGoodBye.js new file mode 100644 index 0000000..5ccc229 --- /dev/null +++ b/assignment4-solution-starter/easier/SpeakGoodBye.js @@ -0,0 +1,25 @@ +// NOTE! The steps in this file are basically identical to the ones you +// performed in the SpeakHello.js file. + +// STEP 6: Wrap the entire contents of SpeakGoodBye.js inside of an IIFE +// See Lecture 52, part 2 + + +// STEP 7: Create an object, called 'byeSpeaker' to which you will attach +// the "speak" method and which you will expose to the global context +// See Lecture 52, part 1 +// var byeSpeaker = + +// DO NOT attach the speakWord variable to the 'byeSpeaker' object. +var speakWord = "Good Bye"; + +// STEP 8: Rewrite the 'speak' function such that it is attached to the +// byeSpeaker object instead of being a standalone function. +// See Lecture 52, part 2 +function speak(name) { + console.log(speakWord + " " + name); +} + +// STEP 9: Expose the 'byeSpeaker' object to the global scope. Name it +// 'byeSpeaker' on the global scope as well. +// xxxx.xxxx = byeSpeaker; diff --git a/assignment4-solution-starter/easier/SpeakHello.js b/assignment4-solution-starter/easier/SpeakHello.js new file mode 100644 index 0000000..a1de3fd --- /dev/null +++ b/assignment4-solution-starter/easier/SpeakHello.js @@ -0,0 +1,24 @@ +// STEP 2: Wrap the entire contents of SpeakHello.js inside of an IIFE +// See Lecture 52, part 2 + + +// STEP 3: Create an object, called 'helloSpeaker' to which you will attach +// the "speak" method and which you will expose to the global context +// See Lecture 52, part 1 +// var helloSpeaker = + +// DO NOT attach the speakWord variable to the 'helloSpeaker' object. +var speakWord = "Hello"; + +// STEP 4: Rewrite the 'speak' function such that it is attached to the +// helloSpeaker object instead of being a standalone function. +// See Lecture 52, part 2 +function speak(name) { + console.log(speakWord + " " + name); +} + +// STEP 5: Expose the 'helloSpeaker' object to the global scope. Name it +// 'helloSpeaker' on the global scope as well. +// See Lecture 52, part 2 +// (Note, Step 6 will be done in the SpeakGoodBye.js file.) +// xxxx.xxxx = helloSpeaker; diff --git a/assignment4-solution-starter/easier/index.html b/assignment4-solution-starter/easier/index.html new file mode 100644 index 0000000..8a5c354 --- /dev/null +++ b/assignment4-solution-starter/easier/index.html @@ -0,0 +1,13 @@ + + + + + Module 4 Solution Starter + + + + + +

Module 4 Solution Starter

+ + diff --git a/assignment4-solution-starter/easier/script.js b/assignment4-solution-starter/easier/script.js new file mode 100644 index 0000000..2c5e0c4 --- /dev/null +++ b/assignment4-solution-starter/easier/script.js @@ -0,0 +1,68 @@ +// ******************************* +// START HERE IF YOU WANT AN EASIER 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". So, the final output +// on the console should look like this: +/* +Hello Yaakov +Good Bye John +Good Bye Jen +Good Bye Jason +Hello Paul +Hello Frank +Hello Larry +Hello Paula +Hello Laura +Good Bye Jim + +WARNING!!! WARNING!!! +The code does NOT currently work! It is YOUR job to make it work +as described in the requirements and the steps in order to complete this +assignment. +WARNING!!! WARNING!!! + +*/ + +// STEP 1: (NOTHING TO DO. ALREADY DONE FOR YOU) +// 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: (NOTHING TO DO. ALREADY DONE FOR YOU) +// 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: (NOTHING TO DO. ALREADY DONE FOR YOU) + // 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: (NOTHING TO DO. ALREADY DONE FOR YOU) + // 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.speak(names[i]); + } else { + helloSpeaker.speak(names[i]); + } +} + +})(); diff --git a/assignment4-solution-starter/harder/SpeakGoodBye.js b/assignment4-solution-starter/harder/SpeakGoodBye.js new file mode 100644 index 0000000..f4c84fb --- /dev/null +++ b/assignment4-solution-starter/harder/SpeakGoodBye.js @@ -0,0 +1,31 @@ +// NOTE! The steps in this file are basically identical to the ones you +// performed in the SpeakHello.js file. + +// STEP 6: Wrap the entire contents of SpeakGoodBye.js inside of an IIFE +// See Lecture 52, part 2 + +(function(window) { + + +// STEP 7: Create an object, called 'byeSpeaker' to which you will attach +// the "speak" method and which you will expose to the global context +// See Lecture 52, part 1 +// var byeSpeaker = +var byeSpeaker = {}; + +// DO NOT attach the speakWord variable to the 'byeSpeaker' object. +var speakWord = "Good Bye"; + +// STEP 8: Rewrite the 'speak' function such that it is attached to the +// byeSpeaker object instead of being a standalone function. +// See Lecture 52, part 2 +byeSpeaker.speak = function(name) { + console.log(speakWord + " " + name); +} + +// STEP 9: Expose the 'byeSpeaker' object to the global scope. Name it +// 'byeSpeaker' on the global scope as well. +// xxxx.xxxx = byeSpeaker; +window.byeSpeaker = byeSpeaker; + +})(window); \ No newline at end of file diff --git a/assignment4-solution-starter/harder/SpeakHello.js b/assignment4-solution-starter/harder/SpeakHello.js new file mode 100644 index 0000000..d76de12 --- /dev/null +++ b/assignment4-solution-starter/harder/SpeakHello.js @@ -0,0 +1,32 @@ +// STEP 2: Wrap the entire contents of SpeakHello.js inside of an IIFE +// See Lecture 52, part 2 +(function(window) {var speakWord = "Hello"; + +// STEP 3: Create an object, called 'helloSpeaker' to which you will attach +// the "speak" method and which you will expose to the global context +// See Lecture 52, part 1 +// var helloSpeaker = +var helloSpeaker = {}; + +// DO NOT attach the speakWord variable to the 'helloSpeaker' object. + + +// STEP 4: Rewrite the 'speak' function such that it is attached to the +// helloSpeaker object instead of being a standalone function. +// See Lecture 52, part 2 +helloSpeaker.speak = function(name) { + console.log(speakWord + " " + name); +} + + + + +// STEP 5: Expose the 'helloSpeaker' object to the global scope. Name it +// 'helloSpeaker' on the global scope as well. +// See Lecture 52, part 2 + +window.helloSpeaker = helloSpeaker; + +}) (window); +// (Note, Step 6 will be done in the SpeakGoodBye.js file.) +// xxxx.xxxx = helloSpeaker; diff --git a/assignment4-solution-starter/harder/index.html b/assignment4-solution-starter/harder/index.html new file mode 100644 index 0000000..b3d4df0 --- /dev/null +++ b/assignment4-solution-starter/harder/index.html @@ -0,0 +1,16 @@ + + + + + Module 4 Solution Starter + + + + + + +

Module 4 Solution Starter

+ + diff --git a/assignment4-solution-starter/harder/script.js b/assignment4-solution-starter/harder/script.js new file mode 100644 index 0000000..b74ecb0 --- /dev/null +++ b/assignment4-solution-starter/harder/script.js @@ -0,0 +1,72 @@ +// ******************************* +// 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". So, the final output +// on the console should look like this: +/* +Hello Yaakov +Good Bye John +Good Bye Jen +Good Bye Jason +Hello Paul +Hello Frank +Hello Larry +Hello Paula +Hello Laura +Good Bye Jim + +WARNING!!! WARNING!!! +The code does NOT currently work! It is YOUR job to make it work +as described in the requirements and the steps in order to complete this +assignment. +WARNING!!! WARNING!!! + +*/ + +// 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 (name in names /* fill in parts of the 'for' loop to loop over names array */) { + + // 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 = + + var firstLetter = names[name].charAt(0); + + flLower = firstLetter.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 (flLower == 'j'/* fill in condition here */) { + byeSpeaker.speak(names[name]); + // byeSpeaker.xxxx + } else { + helloSpeaker.speak(names[name]); + // helloSpeaker.xxxx + } +} +})(); \ No newline at end of file