in this javascript when I display the totalScore and possibleScore is it possible to…
- User
in this javascript when I display the totalScore and possibleScore is it possible to have a button to start a new game - this time using data from a cat.json file rather than main.json '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 let roundScores = [];
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!"); // Display "Game Over" message const resultsDiv = document.getElementById("results");
const finalScores = `Total Score: ${totalScore} points out of a possible ${possibleScore}<br><br>`; const scoreLine = createScoreLine(totalScore, possibleScore); const paintingDiv = document.getElementById("painting"); paintingDiv.style.backgroundColor = "#fff"; // Set background color directly paintingDiv.innerHTML = finalScores; // Update content with innerHTML paintingDiv.appendChild(scoreLine); // Add individual round scoresresultsDiv.innerHTML += "Painting Scores:"; roundScores.forEach((roundScore, index) => { resultsDiv.innerHTML +=
Round ${index + 1}: ${roundScore} points<br>; });//remove painting info const infoDiv = document.getElementById("info");
if (infoDiv) { infoDiv.remove(); } else { console.error("Div element with ID 'info' not found!"); }
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 = ; callback(FeatureID); }) .catch((error) => console.error("Error fetching data: ", error)); }
- Assistant
Yes, it is possible to have a button to start a new game using data from a cat.json file. You can create a new button similar to the 'Next' button and add an event listener to fetch data from the cat.json file and start a new game with the cat images. Here's an example of how you can add a button for starting a new game with cat images:
// Create a new game button const newGameButton = document.createElement("button"); newGameButton.textContent = "Start New Game with Cats"; // Customize button text newGameButton.classList.add("button"); // Add class 'button' newGameButton.addEventListener("click", () => { // Fetch data from cat.json file and start a new game with cat images fetch("cat.json") .then((response) => response.json()) .then((jsonData) => { // Reset game variables and start a new game with cat images totalScore = 0; possibleScore = 0; imageIndex = 0; roundScores = []; // Fetch street details with cat images fetchStreetDetails((fetchedFeatureID) => { updateImage(fetchedFeatureID, PictureURL); }); }) .catch((error) => console.error("Error fetching data: ", error)); }); // Add the new game button to the buttons div const buttonsDiv = document.getElementById("buttons"); buttonsDiv.appendChild(newGameButton); - User
- Assistant
The error you're encountering is likely due to the fact that the
infoDivelement is not found when trying to update itsinnerHTMLin theupdateImagefunction. To resolve the error, you can check if theinfoDivelement exists before setting itsinnerHTML. Here's an updated version of theupdateImagefunction with the addition of this check:function updateImage(FeatureID, PictureURL) { const infoDiv = document.getElementById("info"); if (!infoDiv) { console.error("Element with ID 'info' not found!"); return; } const infoHTML = Description; infoDiv.innerHTML = infoHTML; const paintingDiv = document.getElementById("painting"); const imageHTML = '<img src="' + PictureURL + '" onclick="this.requestFullscreen()" style="width: 90%;" class="center">'; console.log("Image URL:", imageHTML); // Log the image URL to the console paintingDiv.innerHTML = imageHTML; }By adding this check for the
infoDivelement, the error should no longer occur when updating the image and description elements. This check ensures that the code only tries to update theinnerHTMLif theinfoDivelement is found in the document. - User
I think no data is being fetched from cat.json can I add a console.log in the fetch function to check what data is fetched for 'Description' 'PictureURL', and 'FeatureID' put it after this line '.catch((error) => console.error('Error fetching data: ', error));'
- Assistant
Yes, you can add a
console.logstatement after the.catchfunction in the fetch call to check the data that is fetched forDescription,PictureURL, andFeatureID. Here's how you can modify the fetch function to include these console logs:fetch("main.json") .then((response) => response.json()) .then((jsonData) => { // Existing code to handle the fetched data // Additional console logs for Description, PictureURL, and FeatureID console.log("Fetched Description: ", Description); console.log("Fetched PictureURL: ", PictureURL); console.log("Fetched FeatureID: ", FeatureID); }) .catch((error) => console.error('Error fetching data: ', error));By adding these
console.logstatements, you'll be able to see the fetched values forDescription,PictureURL, andFeatureIDin the browser console. This will help you determine if the data is being successfully fetched from thecat.jsonfile or if there are any issues with retrieving the data.