JavaScript代写 | Solving Problems Using Functions


TODO 1: Modify the thumbnail Event Listener
At the very end of the thumbnail “click” event listener, “stringify” the image variable and save it
to localStorage. “Stringify” using the JSON.stringify() function. The localStorage key you should
use is stored in the storageKey variable.
By adding this syntax to the end of the “click” event listener, the gallery always saves the last –
selected thumbnail. This will allow the gallery to load the last-visited photo, thus saving the
user’s place.
TODOs 2 & 3: Modify the code that initially loads the main image and description
The gallery should check whether a “last-visited” image has been saved in localStorage in order
to determine whether to load that particular image or the first image in the gallery.
Your program should now include a conditional to check whether an image reference has been
saved. Remember, the localStorage key you should be checking is saved in
the storageKey variable. If an image has been saved, your program should leverage the
provided loadSavedImage() function. Otherwise, your program should load the first image to
the main image and description, as it did before.
Helpful Documentation

If you’re wondering what your localStorage key should be:
Remember to save the localStorage reference using the key variable (storageKey) provided.
If you’re having trouble saving an associative array to localStorage:
When saving an associative array to localStorage, you need to convert it to a JSON string. You
can do so using the built-in JavaScript function JSON.stringify like so:
var array = {“firstName”: “Bill”, “lastName”: “Hendrickson”};
localStorage.setItem(key, JSON.stringify(array));

If you’re wondering how to check whether a localStorage key exists:
To check whether a key has been saved to localStorage, you can simply check for its existence
using a conditional statement, like so:
if (localStorage.getItem(“keyToCheck”)) {
// Instructions to run if key exists
} else {
// Instructions to run if key does not exist
Course Project, Part Two — Add User-Defined Functions to Your Program
Add User-Defined Functions to Your Program
About this project
The new loadSavedImage function in the image gallery works great! You decide to get creative
and implement two new user-defined functions to add “next” and “previous” buttons to allow
users to browse through the image gallery.
The code necessary for the “next” and “previous” buttons is nearly identical to the code for the
thumbnail-clicked event listeners. To reduce code duplication, we can replace the existing
event listener code with a new function setImageAndDescription(image, imageElement,
descriptionElement) , which:
• Sets the main image element’s src and alt attributes.



本网站支持淘宝 支付宝 微信支付  paypal等等交易。如果不放心可以用淘宝交易!

E-mail:  微信:itcsdx