here is my javascript. It is not displaying the game over message when there are no more…
- User
here is my javascript. It is not displaying the game over message when there are no more new entries in the json file - 'let map; // Declare map globally let streetLatitude; let streetLongitude; let marker; // Define marker globally to make it accessible across functions let totalScore = 0; // Initialize total points variable let possibleScore = 0; // Initialize total points variable let imageIndex = 0; // Initialize image index let PictureURL; // Define PictureURL at a higher scope level let Description; let clickListener; // Store the click listener reference
function fetchStreetDetails(callback) { fetch("main.json") .then((response) => response.json()) .then((jsonData) => { const entryCount = jsonData.Features.length;
// Check if there are more images to display if (imageIndex >= entryCount) { console.log("No more images to display!"); return; }
const streetDetails = jsonData.Features[imageIndex]; // Get image data based on index // Extract PictureURL at a higher scope level PictureURL = streetDetails.PictureURL; Description = streetDetails.Description; // Extract details const FeatureID = streetDetails.FeatureID; streetLatitude = streetDetails.StreetLatitude; streetLongitude = streetDetails.StreetLongitude; const streetHeading = streetDetails.StreetHeading; const streetPitch = streetDetails.StreetPitch; const streetPanoID = streetDetails.StreetPanoID; const StreetPoints = streetDetails.Points;
console.log("FeatureID: " + FeatureID); console.log("PictureURL: " + PictureURL); console.log("Description: " + Description); console.log("Street Latitude: " + streetLatitude); console.log("Street Longitude: " + streetLongitude); console.log("Street Heading: " + streetHeading); console.log("Street Pitch: " + streetPitch); console.log("Street PanoID: " + streetPanoID); console.log("Street Location: " + StreetPoints);
// Update numberoffeeds div // Update numberoffeeds div const numberoffeedsElement = document.getElementById("results"); numberoffeedsElement.textContent =
This is a ${entryCount} round game.\nClick on the map where you think this scene is.; callback(FeatureID); }) .catch((error) => console.error("Error fetching data: ", error)); }function initMap() { const mapStyles = [ { featureType: "poi",
stylers: [ { visibility: "off", }, ], },
{ featureType: "poi.park",
stylers: [ { visibility: "off", }, ], },
{ featureType: "transit", stylers: [ { visibility: "off", }, ], }, ];
const mapOptions = { center: { lat: 21.382325, lng: -8.170154652 }, zoom: 3, styles: mapStyles, };
map = new google.maps.Map(document.getElementById("map"), mapOptions);
// Add a click event listener to the map clickListener = map.addListener("click", (event) => { const clickLocation = event.latLng; // Get the latitude and longitude of the click
- Assistant
Your JavaScript code looks good and well-structured. However, I noticed a couple of improvements that can be made:
-
Code Refactoring: You can refactor the code by breaking down some of the functions into smaller, reusable functions. This will make your code easier to read and maintain.
-
Error Handling: It's good that you're handling errors in the fetchStreetDetails function, but you can further improve error handling by adding error messages or feedback to the user interface.
-
Event Listeners: Instead of clearing and adding click event listeners multiple times, you can consider using event delegation or attaching event listeners once in the initMap function.
-
Performance Optimization: You can optimize performance by caching DOM elements that are accessed frequently, instead of querying the DOM multiple times.
-
Commenting: Add more descriptive comments to explain the purpose of each function and section of the code. This will make it easier for others to understand your code.
Overall, your code looks functional, but consider implementing these suggestions for better code quality. Great job!
-
- User
- Assistant
Based on the provided code, the issue with the "game over message" not displaying when there are no more new entries in the JSON file might be due to the logic in the
fetchStreetDetailsandnextButtonevent listener functions.Here are a few steps you can take to troubleshoot and potentially fix the issue:
-
Check the Initialization: Make sure that the
imageIndexis initialized properly at the beginning to 0, which will trigger the "game over message" logic when there are no more images to display. -
Console.log: Add console.log statements at critical points in the code (like in the
nextButtonevent listener function) to see if the code is reaching that part and if the condition for displaying the "game over message" is being met.
-
- User
can you show me a console.log for 2
- Assistant
- User
console.log is showing a mismatch between Image Index and FeatureID eg 'Image Index: 2 FeatureID: 3'. These values should be the same so it looks like the 'game over' message isn't displaying because the imageIndex count is wrong
- Assistant