Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter is not of type 'Node'

Could someone explain why is this not allowed in functions? shouldnt it return a “node” when u return a value from a functions. I tried to refrector code for better cohesion , in strongly typed languges the logic should work if functions are returning the value =/. Am i missing something here ? appreciate any help :slight_smile:

//failed

function addToTargetList()
{
    targetList.appendChild(addItemsToList);//error
    text.value ="";
}


function addItemsToList(){
     var list = document.createElement("li");
     list.classList.add("items");
     list.textContent = text.value;
     return list;
}


//will work but bad cohesion

function addToTargetList()
{
    var list = document.createElement("li");
    list.classList.add("items");
    list.textContent = text.value;
    targetList.appendChild(addItemsToList);
    text.value ="";
}

What type does addItemsToList return?

Edit: I probably wont remember to check this again in a timely fashion, but here’s a couple of hints:

  1. Be aware that your ‘bad cohesion’ function as presented doesn’t work either - it has the same bug as the better code.
  2. Use ‘typeof’ to determine what “addItemsToList” is. (syntax is: console.log(typeof addItemsToList)). You are correct that if you call the function it should (and would!) return a Node.
  3. Consider how you would rewrite your addItemsToList() function if it took a parameter, and how the call to that function would change (hint, hint!). You don’t need to pass it a parameter, this thought experiment is just to help you find the bug.

thanks i will look into it , the typeof syntax is very useful :slight_smile: