0

I am extremely new to programming and an assignment from class was to create a voting website. I was able to create variables and put them into local storage, as such:

var eventName = document.getElementById("eventName").value;
document.getElementById("p1").innerHTML = (eventName);
localStorage.setItem("eventName", eventName)
localStorage.getItem("eventName")

Now, we were supposed to include all of this into an array so that we can get multiple eventNames. My teacher is never really clear with his instructions, so this is what I got now:

var eventName = [];
var index ;

function submitNewEvent() {

eventName[index] = document.getElementById("eventName").value;
index = index + 1;
var eventNmString = JSON.stringify (eventName);
localStorage.setItem("eventName", JSON.stringify (eventName));
localStorage.getItem("eventName")
array = JSON.parse(localStorage.getItem("eventName"));

array = parse;
var output = "";
for (var i=0, array.length > i; i++){
output += "<p>"+array[i];
}
document.getElementById("p1").innerHTML = (output);

I would really appreciate any help if anyone can explain to me what I did wrong.

1
  • 1
    What is array = parse; supposed to do? There is no parse variable shown, so that should give an error. var index; should be var index = 0;. Also, why are you using localStorage at all? In the code shown the only time you retrieve the value with .getItem() is immediately after storing it with .setItem(), so that seems pointless - the idea with localStorage is to store items now and retrieve them in some later session. Commented Apr 11, 2017 at 1:50

1 Answer 1

1

Here's the syntax error (the error message should point to that line as well):

for (var i=0, array.length > i; i++){
//          ^

The array.length expression is not a valid identifier name, like i is (in a statement like var i=0, array=5; or so). You wanted to use a semicolon there:

for (var i=0; i < array.length; i++){
//          ^
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.