1

以下代码在 chrome 开发人员工具中给了我一个错误:

“未捕获的语法错误 - 意外标记”

具体来说,错误出现在填充标头函数中

var notraw  = JSON.parse(arrayraw)

在第一个 if 语句中,在

parsed = JSON.parse(retrieved); //var test is now re-loaded!

这些错误在以前的迭代中没有出现。有谁知道为什么?

// This statement should be ran on load, to populate the table
// if Statement to see whether to retrieve or create new
if (localStorage.getItem("Recipe") === null) { // if there was no array found
    console.log("There was no array found. Setting new array...");
    //Blank Unpopulated array
    var blank = [
        ["Untitled Recipe", 0, 0]
    ];
    // Insert Array into local storage
    localStorage.setItem("Recipe", JSON.stringify(blank));
    console.log(blank[0]);
} else {
    console.log("The Item was retrieved from local storage.");
    var retrieved = localStorage.getItem("Recipe"); // Retrieve the item from storage
    // test is the storage entry name
    parsed = JSON.parse(retrieved); //var test is now re-loaded!
    // we had to parse it from json
    console.log(parsed)
}
// delete from array and update

function deletefromarray(id) {
    var arrayraw = localStorage.getItem("Recipe")
    var notraw = JSON.parse(arrayraw)
    console.log(notraw[id])
    notraw.splice(id, 1);
    localStorage.setItem("Recipe", JSON.stringify(notraw));
}
// This adds to local array, and then updates that array.

function addtoarray(ingredient, amount, unit) {
    var arrayraw = localStorage.getItem("Recipe")
    var notraw = JSON.parse(arrayraw)
    notraw.push([ingredient, amount, unit]);
    localStorage.setItem("Recipe", JSON.stringify(notraw));
    var recipeid = notraw.length - 1
    console.log("recipe id:" + recipeid)
}
//The calculation function, that gets the ingredients from the array, and calculates what ingredients are needed

function calculate(multiplier) {
    alert("Calculate function was called")
    var length = recipearray.length - 1;
    console.log("There are " + length + " ingredients.");
    for (i = 0; i < length; i++) {
        console.log("raw = " + recipearray[i + 1][1] + recipearray[i + 1][2]);
        console.log("multiplied = " + recipearray[i + 1][1] / recipearray[0][2] * multiplier + recipearray[i + 1][2]);
    }
}
// The save function, This asks the user to input the name and how many people the recipe serves. This information is later passed onto the array.

function save() {
    var verified = true;
    while (verified) {
        var name = prompt("What's the name of the recipe?")
        var serves = prompt("How many people does it serve?")
        if (serves === "" || name === "" || isNaN(serves) === true || serves === "null") {
            alert("You have to enter a name, followed by the number of people it serves. Try again.")
            verified = false;
        } else {
            alert("sucess!");
            var element = document.getElementById("header");
            element.innerHTML = name;
            var header2 = document.getElementById("details");
            header2.innerHTML = "Serves " + serves + " people"
            calculate(serves)
            var arrayraw = localStorage.getItem("Recipe")
            var notraw = JSON.parse(arrayraw)
            notraw.splice(0, 1, [name, serves, notraw.length])
            localStorage.setItem("Recipe", JSON.stringify(notraw))
            return;
        }
    }
}
// the recipe function processes the inputs for the different ingredients and amounts.

function recipe() {
    // Declare all variables
    var ingredient = document.getElementById("ingredient").value;
    var amount = document.getElementById("number").value;
    var unit = document.getElementById("unit").value;
    var count = "Nothing";
    console.log("Processing");
    if (isNaN(amount)) {
        alert("You have to enter a number in the amount field")
    } else if (ingredient === "" || amount === "" || unit === "Select Unit") {
        alert("You must fill in all fields.")
    } else if (isNaN(ingredient) === false) {
        alert("You must enter an ingredient NAME.")
    } else {
        console.log("hey!")
        // console.log(recipearray[1][2] + recipearray[1][1])
        var totalamount = amount + unit
        edit(ingredient, amount, unit, false)
        insRow(ingredient, totalamount) // post(0,*123456*,"Fish")
    }
}

function deleteRow(specified) {
    // Get the row that the delete button was clicked in, and delete it.
    var inside = specified.parentNode.parentNode.rowIndex;
    document.getElementById('table').deleteRow(inside);
    var rowid = inside + 1 // account for the first one being 0
    console.log("An ingredient was deleted by the user: " + rowid);
    // Remove this from the array.
    deletefromarray(-rowid);
}

function insRow(first, second) {
    //var first = document.getElementById('string1').value;
    //var second = document.getElementById('string2').value;
    // This console.log("insRow: " + first)
    // Thisconsole.log("insRow: " + second)
    var x = document.getElementById('table').insertRow(0);
    var y = x.insertCell(0);
    var z = x.insertCell(1);
    var a = x.insertCell(2);
    y.innerHTML = first;
    z.innerHTML = second;
    a.innerHTML = '<input type="button" onclick="deleteRow(this)" value="Delete">';
}

function populateheader() {
    // Populate the top fields with the name and how many it serves
    var arrayraw = localStorage.getItem("Recipe")
    var notraw = JSON.parse(arrayraw)
    var element = document.getElementById("header");
    element.innerHTML = notraw[0][0];
    var header2 = document.getElementById("details");
    // if statement ensures the header doesn't say '0' people, instead says no people
    if (notraw[0][1] === 0) {
        header2.innerHTML = "Serves no people"
    } else {
        header2.innerHTML = "Serves " + notraw[0][1] + " people"
    }
    console.log("Now populating Header, The Title was: " + notraw[0][0] + " And it served:         " + notraw[0][1]);
}

function populatetable() {
    console.log("Now populating the table")
    // Here we're gonna populate the table with data that was in the loaded array.
    var arrayraw = localStorage.getItem("Recipe")
    var notraw = JSON.parse(arrayraw)
    if (notraw.length === 0 || notraw.length === 1) {
        console.log("Array was empty.")
    } else {
        var count = 1;
        while (count < notraw.length) {
            amount = notraw[count][1] + " " + notraw[count][2]
            insRow(notraw[count][0], amount)
            console.log("Inserted Ingredient: " + notraw[count][0] + notraw[count][1])
            count++;
        }
    }
}
4

0 回答 0