gram variable is a string. But when you want the concatenated string to be displayed on the page, you need to explicitly update the value to the DOM element using innerHTML for example.

You are missing break statement in your switch case.

You need to give a event handler that has a signature like
btn.onclick = function(event){}.
But you have a signature like this btn.onclick = diagram("", listArray, 10) . When you have something like this, your method wouldn't get the parameters you intend to pass.