wyc
wyc

Reputation: 55303

Why are the arguments of the following function being ignored?

I created a function to replace strings in nested arrays recursively:

function replaceString(tree, oldStr, newStr) {
  for (var i = 0; i < tree.length; i++) {
    if (Array.isArray(tree[i])) {
      replaceString(tree[i])
    } else {
      tree[i] = tree[i].replace(oldStr, newStr)
    }
  }
}

function replaceQuotes(tree, callback) {
  var oldStr = /"(?=\b)/g
  var newStr = '“'

  replaceString(tree, oldStr, newStr)

  callback(null, tree) 
}

But for some reason the arguments are not being recognized, which is strange because if I do this:

tree[i] = tree[i].replace(regex, str).replace(/"(?=\b)/g, '“')

in the findLastString function, works perfectly fine.

What could be the problem?

This is the input:

[ 'markdown',
  [ 'para', '"a paragraph"' ],
  [ 'hr' ],
  [ 'para', '\'another paragraph\'' ],
  [ 'bulletlist',
    [ 'listitem', '"a list item"' ],
    [ 'listitem', '"another list item"' ] ] ]

Upvotes: 0

Views: 49

Answers (1)

hjpotter92
hjpotter92

Reputation: 80647

In your recursive call, you are not passing the 2 arguments:

replaceString(tree[i])

should be:

replaceString(tree[i], oldStr, newStr)

Upvotes: 5

Related Questions