Generate a new JSON by manipulating the data of another JSON

2

I was participating in an internship test and the following problem was proposed to me:

I would have to read the JSON below:

[ 
{ "nome":"Jabba, the Hutt", "jedi":false, "sistemas":[ "Tatooine" ] },
{ "nome":"Chewbacca", "jedi":false, "sistemas":[ "Kashyyk" ] },
{ "nome":"Han", "jedi":false, "sistemas":[ "Tatooine", "Coruscant" ] },
{ "nome":"Leia", "jedi":true, "sistemas":[ "Alderaan", "Endor" ] },
{ "nome":"Luke", "jedi":true, "sistemas":[ "Tatooine", "Dagobah" ] },
{ "nome":"Yoda", "jedi":true, "sistemas":[ "Kashyyk", "Dagobah" ] },
{ "nome":"Obi-Wan", "jedi":true, "sistemas":[ "Coruscant", "Mustaphar" ] },
{ "nome":"Darth Vader", "jedi":false, "sistemas":[ "Tatooine", "Mustaphar" ] }]

And I would have to create a new JSON that would look something like this:

[{"nome": Darth Vader, "contato":["Luke", "Leia" ....]},{}]

It would be a JSON that would have in each field the character's name and an array that would be the contacts.

Now the condition for a character to enter the contact array of another character is:

  • This character is not the same character as the

  • 2 characters can be contacts between them if both are "jedi": true OR if they have any system in common. For example Jabba, the Hutt will be Han contact because both have Tatooine in their arrays in the systems field.

Here's my code:

var v = [ 
{ "nome":"Jabba, the Hutt", "jedi":false, "sistemas":[ "Tatooine" ] },
{ "nome":"Chewbacca", "jedi":false, "sistemas":[ "Kashyyk" ] },
{ "nome":"Han", "jedi":false, "sistemas":[ "Tatooine", "Coruscant" ] },
{ "nome":"Leia", "jedi":true, "sistemas":[ "Alderaan", "Endor" ] },
{ "nome":"Luke", "jedi":true, "sistemas":[ "Tatooine", "Dagobah" ] },
{ "nome":"Yoda", "jedi":true, "sistemas":[ "Kashyyk", "Dagobah" ] },
{ "nome":"Obi-Wan", "jedi":true, "sistemas":[ "Coruscant", "Mustaphar" ] },
{ "nome":"Darth Vader", "jedi":false, "sistemas":[ "Tatooine", "Mustaphar" ] }];

function verificaSist(){
    var verifica = false;
    for(var i in arguments[0]){
        for(var b in arguments[1]){
            if(arguments[0][i]===arguments[1][b]){
                verifica = true;
                break;      
            }   
        }
        if(verifica == true){
            break;
        }
    }
    if(verifica == true){
        return true;
    }
    else{
        return false;
    }

  }

function criaAgenda(v){
  var agenda=[];
  var contato = []
  for(var personagem1 of v){
    for(var personagem2 of v){
        var sist = [personagem1.sistemas, personagem2.sistemas];
        var d = verificaSist.apply(null,sist);

        if(((personagem1.jedi == true && personagem2.jedi==true)|| d == true) && personagem1.nome !== personagem2.nome){
            contato.push(personagem2.nome);
        }
    }
    agenda.push({"nome":personagem1.nome, "contato":contato=[]});

  }
  return JSON.stringify(agenda);
}
console.log(criaAgenda(v));

The problem I see is this if :

if(((personagem1.jedi == true && personagem2.jedi==true)|| d == true) && personagem1.nome !== personagem2.nome)

I just can not think of anything better.

    
asked by anonymous 03.09.2018 / 21:37

1 answer

1

The code itself would work correctly, except for placing the contato array in the wrong place. The correct one would be to reset this array every time the first for in the criaAgenda function:

for(var personagem1 of v){
   var contato = [];
   for(var personagem2 of v){
      ...
   }
}

And in% w_thread only report array:

agenda.push({"nome":personagem1.nome, "contato":contato});

Another thing is expendable codes in the agenda.push function. Instead of doing verificaSist() s, just return break when something matches, otherwise return true , without having to use the control variable false .

When doing verifica in the function, the loop is already suspended without needing return . Other things that can wipe the code is to check if the variable is break only with the variable itself, without true , for example:

== true is the same as if(d == true) and if(d) is the same as if(d == false)

See what the if(!d) function would look like without excess and compare with its original:

function verificaSist(){
   for(var i in arguments[0]){
      for(var b in arguments[1]){
         if(arguments[0][i]===arguments[1][b]) return true;
      }
   }
   return false;
}

Much simpler, no?!

Code in operation:

var v = [ 
{ "nome":"Jabba, the Hutt",   "jedi":false, "sistemas":[ "Tatooine" ] },
{ "nome":"Chewbacca",         "jedi":false, "sistemas":[ "Kashyyk" ] },
{ "nome":"Han",               "jedi":false, "sistemas":[ "Tatooine", "Coruscant" ] },
{ "nome":"Leia",              "jedi":true,  "sistemas":[ "Alderaan", "Endor" ] },
{ "nome":"Luke",              "jedi":true,  "sistemas":[ "Tatooine", "Dagobah" ] },
{ "nome":"Yoda",              "jedi":true,  "sistemas":[ "Kashyyk", "Dagobah" ] },
{ "nome":"Obi-Wan",           "jedi":true,  "sistemas":[ "Coruscant", "Mustaphar" ] },
{ "nome":"Darth Vader",       "jedi":false, "sistemas":[ "Tatooine", "Mustaphar" ] }];

function verificaSist(){
   for(var i in arguments[0]){
      for(var b in arguments[1]){
         if(arguments[0][i]===arguments[1][b]) return true;
      }
   }
   return false;
}

function criaAgenda(v){
  var agenda=[];
  
  for(var personagem1 of v){
    var contato = [];
    for(var personagem2 of v){
        var sist = [personagem1.sistemas, personagem2.sistemas];
        var d = verificaSist.apply(null,sist);

        if( (personagem1.jedi && personagem2.jedi || d) && personagem1.nome !== personagem2.nome){
            contato.push(personagem2.nome);
        }
    }

    agenda.push({"nome":personagem1.nome, "contato":contato});

  }
  return JSON.stringify(agenda);
}

console.log(JSON.parse(criaAgenda(v)));
    
03.09.2018 / 22:49