the code file is for the script.js and please follow the assignment instructions and mail mw
[email protected]Microsoft Word - Practice 4.2.docx Page 1 Code.Zero Practice Exercise 4.2 This exercise is based on material in: Lecture 5. Modular Code Practice Exercise 4.3 (Video) Again, we are going to write our JavaScript in the script.js file, and execute the file by opening container.html within a browser. PRACTICE 4.2 Simon’s lecture showed how to write and call a function. In the video Practice Exercise 4.3 we continued with our “lousy vending machine” and added some functionality and also defined and called a function. With short programs, it is hard to see why a function saves much time. But as your programs become more complex, you will see that functions are nice ways to compartmentalize some code. For this practice, we will again provide an outline for the logic of the program that will guide you through the development of your next project. Situation: Let’s continue with our temperature conversion website. You have already written the code to convert temperatures based on a condition (whether the user enters “C” or “F”). For practice, take the two formulas for Celsuius to Farnheit and Farenheit to Celsius and write them as functions. Then call the functions from within the switch statement (or if statement if you used that in your last practice.) Let’s talk through the program in English. Note that you already have most of this from the last practice exercise. 1. Prompt the user for a temperature and store it in a variable. 2. Then prompt the user for a conversion type. (such as, “Enter C if Celsius or F if Farenheit.”) 3. If the user enters “C” then call the CentToFaren() function 4. If the user enters “F” then use the FarenToCent() function. 5. If the user enters anything other than “C” or “F” then provide a response such as “Illegal input” or something else that you like. 6. Write the CentToFaren() function using the C to F formula 7. Write the FarenToCent() function using the F to C formula 8. Display the converted result for the user with a label of some sort. This will not actually do anything that the last version of this program did not do! But, it will give you some practice writing functions. Page 2 Remember that a function that you define will not run automatically – you’ll need to call the function in your code and also pass an argument to the function. Below is a reminder of the general format. Microsoft Word - Practice 5.2 and 5.3.docx Page 1 Code.Zero Practice Exercise 5.2 & 5.3 This exercise is based on material in: • 6. Iteration: Writing Loops • 7. More about Strings • Practice Exercise 5.1 (Video) Again, we are going to write our JavaScript in the script.js file, and execute the file by opening container.html within a browser. PRACTICE 5.2 This is our final wrap-‐‑up to the temperature converter project. You already have a lot of code written for the temperature converter. Let’s add a big loop that will function as a way to enter numerous values to convert. Note that you already have most of this from the last practice exercise. 1. Prompt the user for a temperature and store it in a variable. 2. Then prompt the user for a conversion type. (such as, “Enter C if Celsius or F if Farenheit.”) 3. If the user enters “C” then call the CentToFaren() function or if the user enters “F” then use the FarenToCent() function. 4. If the user enters anything other than “C” or “F” then provide a response such as “Illegal input” or something else that you like. 5. Write the CentToFaren() function using the C to F formula 6. Write the FarenToCent() function using the F to C formula 7. Display the converted result for the user with a label of some sort. 8. Prompt the user for another temperature 9. Loop back and repeat steps 2-‐‑10 until the user enters “Q” or another phrase to end the program Page 2 PRACTICE 5.3 1. For a challenge activity, try to create a set of loops and functions that will output the text-‐‑based equivalent of an 8x8 chess board. (Use “X” for a black square and a underscore “__” for a white square.) Something like this: X__X__X__X__ __X__X__X__X X__X__X__X__ __X__X__X__X X__X__X__X__ __X__X__X__X X__X__X__X__ __X__X__X__X Of course, you can solve the problem by just writing this all out. But try to create this using some loops inside of loops. 1. You can repeat the action of “X__” to make a row. If you are very clever, you can write a function. 2. You can repeat every two rows to make the full board. 3. You can use the built-‐‑in function that we used earlier to try to write this out to a web page rather than using the alert function. document.getElementById(“first”).innerHTML 4. If you get all of that, why not look for a character to make a black and white square, rather than an X and an underscore! Page 1 Code.Zero Practice Exercise 6.3 This exercise is based on material in: • Lecture 8. Collections • Video Demonstration 6.1 (Video) • Video Demonstration 6.2 (Video) Again, we are going to write our JavaScript in the script.js file, and execute the file by opening container.html within a browser. PRACTICE 6.3 You saw in video 6.1 and video 6.2 how useful an array can be. In some ways it can be thought of as a little data record – it keeps together a lot of related information. You should now be able to refer to a specific item in an array such as myArray[2] that would refer to the third element in an array. We also showed how you can use a switch statement to select an array based on an element in the array. We also showed you how to copy and array using a for loop. Although our battle game was not really a ton of fun to play, it enabled us to build our skill with arrays. We will build a new (probably equally non-fun) game called “Race up the Building.” The user will see how long it takes for a contestant to run up a selected building. For example, can Dave run up the Eiffel tower faster than Sally can run up the Empire State Building? Using these elements, for Practice 6.2, you can start with much of the same code and modify it. 1. Use the same data from the last game, with elements representing name, strength, speed, species var d = ["Dave",23,34,"elf"]; var s = ["Sally",44,12,"warrior"]; var r = ["Richie",33,14,"warrior"]; 2. Create 3 building arrays that will each have three elements: name, height in feet, country. Eiffel Tower, 984, France Empire State Building, 1250, USA Page 2 Burf Kalifa, 2717, Dubai 3. Prompt the user for a player1 and player 2 (as we did in the video) 4. Prompt the user for a building1 and building 2 5. Here is the good stuff -- you can use a lot of the code that I showed in the video. a. Copy the elements from the first person selected into the player1 array b. Copy the elements from the second person selected in the player2 array c. Copy the elements from the first building selected in a new building1 array d. Copy the elements from the second building selected in a new building2 array 6. For the race: divide the height of the first selected building by the first player’s speed. That will give you roughly how long it takes the first player to run up that building. You will have to access the height element from the building array and the speed element from the player array. 7. Now divide the height of the second selected building by the second player’s speed. That will give you roughly how long it takes the second player to run up that building Conceptually, you will have something like this: • Dave running up the Burj Khalifa = 2717/34 = 79.9 • Richie running up the Eiffel Tower = 984/14 = 70 8. The lowest number wins the race. Make sure you announce the winner!