0
var input = [  "KittenService: ",   "Leetmeme: Cyberportal",   "Cyberportal: Ice",   "CamelCaser: KittenService",   "Fraudstream: Leetmeme",   "Ice: "];

var output = [];

function valid(input) {
  for(var i = 0; i < input.length; i++) {
    var array = input[i].trim().split(':');
    var packageName = array[0].trim();
    var dependencyName = array[1].trim();
    if(array.length > 1 && dependencyName === '') {

      if(output.indexOf(packageName) === -1) {
        output.push(packageName);
      }
      else {
        return;
      }
    }
    else if(array.length > 1 && dependencyName !== '') {
      if (output.indexOf(dependencyName) === -1) { 
      output.push(dependencyName); 
      if(output.indexOf(dependencyName) > -1) {
        if(output.indexOf(packageName) > -1) {
         continue;
         }
         else {
           output.push(packageName);
         }
       }
      }
      else if(output.indexOf(dependencyName) > -1) {
        output.push(packageName);
      }
    }
  }
  return output.join(', ');
 }
 valid(input);

I am trying to figure out way to make the output to become

"KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream"

Right it logs

'KittenService, Cyberportal, Leetmeme, Ice, CamelCaser, Fraudstream'

I am not sure how to make all the input with dependencies to pushed before input with dependencies.

1 Answer 1

2

Problem was just that you were returning if no package name instead of using a continue.

var input =[ "KittenService: CamelCaser", "CamelCaser: " ]

var output = [];

function valid(input) {
  for(var i = 0; i < input.length; i++) {
    var array = input[i].trim().split(':');
    var packageName = array[0].trim();
    var dependencyName = array[1].trim();
    if(array.length > 1 && dependencyName === '') {

      if(output.indexOf(packageName) === -1) {
        output.push(packageName);
      }
      else {
        continue;
      }
    }
    else if(array.length > 1 && dependencyName !== '') {
      if (output.indexOf(dependencyName) === -1) { 
        output.push(dependencyName); 
        if(output.indexOf(dependencyName) > -1) {
          output.push(packageName);
        }
      }
    }
  }
  return output;
}
console.log(valid(input));

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

1 Comment

Thanks for input. Didn't know you could do that.

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.