In this javascript why does the '// If not a new personal best, display the saved…
- User
In this javascript why does the '// If not a new personal best, display the saved personal best station.innerHTML +=
<br><br>Personal Best: ${savedPersonalBest.toFixed(2)} miles;' not display the saved PersonalBest score - //display game score station.innerHTML =Well done!<br><br> You've found all the paintings! <br><br> ${roundDistances .map((distance, index) =>Round {distance.toFixed(2)} miles) .join("<br>")}<br> <br>Total distance: ${totalDistance.toFixed(2)} miles.;// Retrieve the previously saved personal best, or set it to 0 if not foundconst savedPersonalBest = localStorage.getItem("personalBest") || 0;
// Check if the current totalDistance is a new personal best if (totalDistance < savedPersonalBest) { // Update the personal best in local storage localStorage.setItem("personalBest", totalDistance);
// Add the personal best statement to the innerHTML station.innerHTML +=
<br><br>Personal Best: ${totalDistance.toFixed(2)} miles; } else { // If not a new personal best, display the saved personal best station.innerHTML +=<br><br>Personal Best: ${savedPersonalBest.toFixed(2)} miles; } - Assistant
In the given JavaScript code, there is a conditional statement that checks if the current
totalDistanceis less than thesavedPersonalBest. If the condition is true, it means that thetotalDistanceis a new personal best score, and the code updates thesavedPersonalBestin the local storage with the new value oftotalDistance.If the condition is false, it means that the
totalDistanceis not a new personal best score, so the code displays the saved personal best score by appending it to theinnerHTMLof thestationelement using the following line of code:station.innerHTML += `<br><br>Personal Best: ${savedPersonalBest.toFixed(2)} miles`;Therefore, the line of code you mentioned does display the saved personal best score when the current
totalDistanceis not a new personal best score.