1seanr 1seanr - 2 years ago 77
Javascript Question

Getting error .append is not a function

I am trying to split up a string on /* and then to split up those segments on */

So that I can separate out all of the comments as I want this code to be able to take all of the comments out of the string and then put it back together.

The problem is though I keep getting this

error which I am pretty sure is because I have made a silly syntax error but I am struggling to find it and any help would be greatly appreciated.


contents = "if for /* else */ . = == === /* return */ function"

var start = /\/\*/gi;
var end = /\*\//gi;
var commentsRemovedSec2 = [];

var commentsRemovedSec1 = contents.split(start);

for (var i = 0; i < commentsRemovedSec1.length; i++) {
var z = ""
var x = commentsRemovedSec1[i]
var y = x.split(start)
z = y[0]


Answer Source

Unfortunately .append() isn't an Array method.

Instead use the Array method .push().


The push() method adds one or more elements to the end of an array and returns the new length of the array. MDN

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download