Need help solving this js problem

Tree Constructor

Have the function TreeConstructor( strArr ) take the array of strings stored in strArr , which will contain pairs of integers in the following format: (i1,i2) , where i1 represents a child node in a tree and the second integer i2 signifies that it is the parent of i1 . For example: if strArr is ["(1,2)", “(2,4)”, “(7,2)”], then this forms the following tree:

which you can see forms a proper binary tree. Your program should, in this case, return the string true because a valid binary tree can be formed. If a proper binary tree cannot be formed with the integer pairs, then return the string false . All of the integers within the tree will be unique, which means there can only be one node in the tree with the given integer value.

Examples

Input: ["(1,2)", “(2,4)”, “(5,7)”, “(7,2)”, “(9,5)”]
Output: true

Input: ["(1,2)", “(3,2)”, “(2,12)”, “(5,2)”]
Output: false

1 Like

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

<script>
Input1 = ["(1,2)", "(2,4)", "(5,7)", "(7,2)", "(9,5)"];
Input2 = ["(1,2)", "(3,2)", "(2,12)", "(5,2)"];
alert("Input1 is "+validate(Input1)+"\nInput2 is "+validate(Input2));
function validate(arg){
var store=[];
var re;
	for(var i=0;i<arg.length;i++){
		re=(eval('list'+arg[i]));
		if(typeof store[re[1]]=='undefined')store[re[1]]=0;		
		if(++store[re[1]]>2)return "invalade";
	}
	function list(a,b){return [a,b];}
	return "valade";
}
</script>