0

I solved this. you are right I was running a function which was the problem. If anyone wants to look at the solution I have attached it below. This was never meant to look nice just function. Thank you for at least looking at it. And to answer question more clearly. This was a debugging assignment as I am working towards a complete understanding through school. As I said I wasn't looking for anyone to solve for me just to guide me so I can solve myself. Most people I have dealt with in the developer community has been super helpful so I simply wanted to reach out for help in my new endeavors. Thank you all for that help. Again I have fully solved all 10 errors which is what the // error# was about and believe will receive 100%. I know debugging probably seems beginner and mundane but we were all beginners once. Again the link to the solved project is below with a copy of the solved and unsolved project if anyone wants to play. This way I offer a benefit to the question having been asked.

https://www.dropbox.com/sh/vngg7j478h6zgcv/SVZF1bep6w

object array:

var flavors = [
{
    "flavor": "Vanilla",
    "favorite": "Yes!",
    "notes": "Great for sundaes."
},
{
    "flavor": "Chocolate",
    "favorite": "Yes!",//first error
    "notes": "All parts chocolate. What's not to love?"
},
{
    "flavor": "Neapolitan",
    "favorite": "No",
    "notes": "I dislike the strawberry chunks."
}
];

code to push the form data to the array:

var saveFlavor = function(){

var fav = getFavorite();   
//error 5 not fixed
var newFlavor = function() {
    flavors.flavor = document.getElementById('flavor').value,
    flavors.favorite = fav, //error 6 not fixed 
    flavors.notes = document.getElementById('notes').value //error 7
    };
    flavors.push(newFlavor);
    console.log(newFlavor)

    location.href="#home";
    document.getElementById('list').innerHTML = "";

};
var save = document.getElementById("submitFlavor"); //error 8
save.addEventListener("click", saveFlavor);

Data called on with:

var showFlavors = function(){
for(var i=0, len=flavors.length; i<len; i++){ //error 2
    var newLi = document.createElement('li');   
    document.getElementById('list').appendChild(newLi);
      var heading =document.createElement('h3');
          heading.innerHTML = flavors[i].flavor;
          newLi.appendChild(heading);
  var pNotes = document.createElement('p');
          pNotes.innerHTML= flavors[i].notes;
          newLi.appendChild(pNotes);
  var pFav = document.createElement('p');
        if(flavors[i].favorite == "Yes!") {
            flav = "Yes";
            } else if (flavors[i].favorite == "No") {
            flav = "No";
    };
    console.log(flav)
    pFav.innerHTML= flav; //error 3 
    newLi.appendChild(pFav);
    console.log(newLi); 
};


};

It does push to the Array but returns as Undefined I also dropboxed this code here if you need to see more. https://www.dropbox.com/sh/ee0usbthykoqllg/cZi_K6zst2

I fixed everything else in this debugging process however I am having trouble solving why I am returning undefined. I am not looking at someone giving me the answer, I am looking for guidance as to what I am doing wrong. It did not work at all when I started and I have fixed the rest just need help with this. Thanks.

2
  • 1
    Could you throw a minimal/SSCCE reproduction of your code into a live demo, where we can play with it, without having to reproduce a page/demo ourselves? And, incidentally, what's with the // error 5 and // error 8 comments? Commented May 12, 2013 at 17:32
  • There's really too many issues with this code for it to be helpful for others in the future, voting to close as "too localised". My advice would be to read a few tutorials on JavaScript. Commented May 12, 2013 at 17:34

1 Answer 1

1

newFlavor isn't an object, it's a function. This is how you create an object:

var newFlavor = {
    flavor: document.getElementById('flavor').value,
    favorite: fav, //error 6 not fixed 
    notes: document.getElementById('notes').value //error 7
};
Sign up to request clarification or add additional context in comments.

16 Comments

A function is an object in javascript, isn't it? ;)
@meze typeof function() { return 1} returns "function"
@Barmar If you want to get technical like that, (function () {}) instanceof Object returns true. meze had a ;) in their comment, meaning they weren't saying a function is literally an object - it's obviously something separate
A function is an object created from the Function constructor which is an instance of Object.
@Ian yeah it was more my understanding on the function itself actually being an object (not really a big JS guy). Deleted the comment incase it causes confusion. However, quick read of this and it all makes sense....functions are objects :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.