简体   繁体   中英

array.push not working - unable to add to array

I'm making a speedcube timer, but can't add times to arrays.

var solves = 0;
var times = [];
var solveCount = document.querySelector(“.nameofDiv”);
var solveTable = document.getElementById(“div”);
var timer = document.querySelector(“.timer”);


function getTime(){
    solves++;
    localStorage.setItem("solvesDone", JSON.parse(solves));
    solveCount.innerHTML = "Solves: " + solves;

    console.log("now doing times")

    var timeFinish = timer.innerHTML.slice(0, 2) + timer.innerHTML.slice(3, 5) + timer.innerHTML.slice(6, 8);
    var timeofSolve = timeFinish;
  
    alert(timeofSolve);
  
    times.push(timeofSolve);
  
    alert(times);
  
    localStorage.setItem("timesDone", JSON.parse(times));

    console.log("now appending");

    var div = document.createElement(div);
  
    div.classList.add("solveBar");
    solveTable.appendChild(div);

    console.log("finished func");
}

It stops when I do times.push .

Full project here for context: https://speedcube-timer.coderguru.repl.co/

Thanks in Advance

your problem is your var declarations, it should be "var" not "Var"

 var solves = 0; var times = [] var solveCount = 'solveCount_TODO'; var solveTable = 'solveTable_TODO'; function getTime() { solves++; //localStorage.setItem("solvesDone", JSON.parse(solves)); solveCount.innerHTML = "Solves: " + solves; console.log("now doing times") var timeFinish = "timeFinish_TODO"; var timeofSolve = timeFinish; alert(timeofSolve) times.push(timeofSolve); alert("Times: " + times); //localStorage.setItem("timesDone", JSON.parse(times)); console.log("now appending") var div = document.createElement(div); div.classList.add("solveBar"); //solveTable.appendChild(div); console.log("finished func") } getTime()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM