0

When i run my code and try the Delete button i get an error message. When you type the name of a game it is supposed to delete that game from the list and update that list. I've checked both the value of the box and the array location. Both are intact and correct but for some reason the error is saying it still cannot be found. I've console logged it and checked if it was a comparison issue. in the delete game function I've tried two ways which are still listed. Any suggestions?

var GameArray = [];
window.onscroll = function() {myfunction()};

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                GameArray = allText.split("\n");
                
            }
        }
    }
    rawFile.send(null);
}

readTextFile("MoreInfo/ListOfGames.txt");

for(var x = 0;x < GameArray.length; x++){
    GameArray[x] = GameArray[x].replace(/-/g,' ');
}

callGames();

function callGames(){
    GameArray.sort();
    for(var x = 0;x < GameArray.length; x++){
        var checkbox = document.createElement("INPUT");
        checkbox.setAttribute("type","checkbox");
        document.getElementById("games").innerHTML += GameArray[x];
        document.getElementById("games").appendChild(checkbox);
        document.getElementById("games").innerHTML += '<br />';
    }
}

function exitButton(){
    window.close();
}

function addGame(){
    var addNewGame = document.getElementById("myText").value;
    GameArray.push(addNewGame);
    document.getElementById("games").innerHTML = "";
    callGames();
}

function deleteGame(){
    var addNewGame = document.getElementById("myText").value;
    var count = 0;


    if (GameArray.indexOf(addNewGame) > -1){
        console.log("Found it!");
    }
    else {
        console.log("Error");
    }


    for(var x = 0;x<GameArray.length;x++){
        if(GameArray[x] == addNewGame){
            if(x == 0){
                count++;
                GameArray.splice(0,1);
                document.getElementById("games").innerHTML = "";
                callGames();
            }
            else{
                count++;
                GameArray.splice(x,x);
                document.getElementById("games").innerHTML = "";
                callGames();
            }
        }
    }
    if(count == 0){
        alert("I'm sorry but the game you have listed has not been found, Please try again.");
    }
}
<div align="right" id="sticky" class="header">
  <h1 id="welcomeUser">Welcome </h1>
  <button id="add" onclick="addGame()">Add</button>
  <input type="text" id="myText" placeholder="Enter Game Name Here" name="textBoxName">
  <button id="delete" onclick="deleteGame()">Delete</button>
  <button id="exitbutton" onclick="exitButton()">EXIT</button>
</div>
<div><p align="center", id=games></p></div>
<form action="scripts/saved.html", id=formValues>
  <h1>Warning pushing submit wont allow any more changes to be made</h1>
  <input type="submit" value="Submit", id=submitButtonCSS>
</form>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • It's not the problem you're asking about, but your code also has [this problem](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). As of the `for` loop at the top level of your code, `GameArray` is guaranteed to be empty. – T.J. Crowder Apr 27 '20 at 16:17
  • *"When i run my code and try the Delete button i get an error message."* I've copied your code into a Stack Snippet and I have no problem adding and deleting games. I add a couple, then tick the box next to one I want to delete, and when I click Delete it works. I don't get any error messages. Please update the question to give specific instructions for how to replicate the problem. – T.J. Crowder Apr 27 '20 at 16:20
  • I dont understand what you mean when you say that GameArray will always be empty. I've called the GameArray and was able to pull information out. I just cant compare the items inside of it – Nicholas McFadden Apr 27 '20 at 18:49
  • I have a pre-generated list with all of the games i want to load, i cannot edit that list though. If i add a game i can then delete it, but not the orignal game from the file ```readTextFile("MoreInfo/ListOfGames.txt");``` – Nicholas McFadden Apr 27 '20 at 18:58

0 Answers0