-1

I'm trying to create a function to process a list of numbers relating to depth using recursion or loops in JavaScript.

The following "input" needs to be processed into the "output", and it needs to work for arbitary lists.

One thing to note is that numbers increase by either 0 or 1 but may decrease by any amount.

var input = [0, 1, 2, 3, 1, 2, 0]

var output =
  [ { number: 0, children: 
      [ { number: 1, children: 
          [ { number: 2, children: 
              [ { number: 3, children: [] } ]
            } 
          ] 
        } 
      , { number: 1, children: 
          [ { number: 2, children: [] } ]
        } 
      ] 
    } 
  , { number: 0, children: [] } 
  ] 

I worked it out myself, although it needs some refinement.

var example = [0, 1, 2, 2, 3, 1, 2, 0]
var tokens = []
var last = 0
const createJSON = (input, output) => {
  if (input[0] === last) {
    output.push({ num: input[0], children: [] })
    createJSON(input.splice(1), output)
  } 
  else if (input[0] > last) {
    last = input[0]
    output.push(createJSON(input, output[output.length-1].children))
  } 
  else if (input[0] < last) {
    var steps = input[0]
    var tmp = tokens
    while (steps > 0) {
      tmp = tmp[tmp.length-1].children
      steps--
    }
    tmp.push({ num: input[0], children: [] })
    createJSON(input.splice(1), tmp)
  }
}
createJSON(example, tokens)
console.log(tokens)

5
  • 1
    I'm trying to create a function Can you show us how you tried to implement your function? Where specifically are you having trouble? Commented Jan 9, 2020 at 23:31
  • Can you elaborate on what you are trying to do? It is very unclear. Commented Jan 9, 2020 at 23:33
  • We expect that you'll do research and make an attempt before posting. Then, when you do post, that you'll show what you've tried and ask a specific question about what you are having trouble with. We're not a code writing service, so help us help you. Commented Jan 9, 2020 at 23:41
  • 2
    you hate placing commas in your output object ? Commented Jan 10, 2020 at 0:16
  • Sorry for not adding my code initially. I didn't add commas as it's only intended to be a representation and I think it's more readable. Commented Jan 10, 2020 at 1:14

2 Answers 2

1

In fact, it's a very simple problem to solve...

var input   = [0, 1, 2, 3, 1, 2, 0]
  , output  = []
  , parents = [output]
  ;
for(el of input)
  {
  let nv = { number:el, children:[] }
  parents[el].push( nv )
  parents[++el] = nv.children  // save the  @ddress of children:[] for adding items on
  }
console.log( output )
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, much more simple than my solution. What does this line do "parents[++el] = nv.children"?
@user3462522 you mean ++el ? this the same incrementation operator as C see = developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Ah, I know this one. I just thought something clever might be happening inside a list. How do I learn to solve recursive problems like this so simply, are there any recommended resources or books?
@user3462522 it is not a recursive problem, the only question was just to know to which element to attach an arriving according to its indentation, from where the idea of a table to keep the addresses of the tables before collecting them. Solving a problem well means first asking the right questions
0

Here's a functional solution based on recursion and Array.prototype.reduce:

const data = [0, 1, 2, 3, 1, 2, 0]
const last = xs => xs.length > 0 ? xs[xs.length - 1] : undefined
const lastD = (d, t, i = 0) => i > d ? t : lastD(d, last(t).children, i + 1)
const buildIt = xs => xs.reduce(
  (a, x) =>((x === 0 ? a : lastD(x - 1, a)).push({ number: x, children: [] }), a),
  []
)

console.log(buildIt(data))
.as-console-wrapper { max-height: 100% !important; top: 0; }

Note: This solution does not depend on the mutation of additional variables for bookkeeping purposes.

Turns out the actual problem was significantly simpler to solve than my initial misinterpretation!

4 Comments

Thanks Tex, it's along the right lines but your first solution doesn't handle decreasing numbers correctly. The second "1" ends up at the root, not as a child of the previous "0".
Ah, I misread the desired output format. Thought that 1 was at the root. Might take another look tomorrow.
It's close, I'm also having difficulties with the decreasing numbers.
user3462522 I've fixed my snippet based on your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.