JavaScript Challenge w/ Test [Job Interview Challenge]

JavaScript Challenge w/ Test [Job Interview Challenge]

In January of 2020, I had a job interview for a Node developer position. I sent my solution, but even though I insisted because I really wanted the job, I never got any response. Anyway, In this article, I'll show how I solved the challenge.

You can check the GitHub repository here.

JavaScript Challenge

Description of this challenge:

Police Search by Name

We have a map with the most wanted criminals.

Map

  • criminals = new HashMap<>();
  • criminals.put("Paul White", "Roger Night, Peter Llong Jr.");
  • criminals.put("Roger Fedexer", "Rob Ford, Pete Lord, Roger McWire");
  • criminals.put("Paul White Jr.", null);
  • criminals.put("Red Fortress", "Roger Rabbit, Ross Winter");
  • criminals.put("Redford Fort", "Red Strong, Red Fort");

The key of the map is his actual name. The value contains possible aliases the criminal has used. The police are interviewing people that have been in contact with one of them. They don't know if the criminal used their name or an alias.

Write a function that returns the most probable criminal having the name (provided by interviewed people) as an input. The function should return a string in the following shape: "First name: name. Aliases: alias0, alias1, aliasN". If there is no match, the response should be "No match". Of course, matching the actual name of the criminal is more meaningful than matching an alias, and having an exact match is more meaningful than a partial match.

For instance:

  • Input -> "paul White" should of course return the first entry.
  • Input -> "Roger" should return the second entry. Two more guys are using "Roger" as an alias, but since the actual name of the second one is Roger, it has more weight.
  • Input -> "Ross" should return the 4th entry. Input -> "white jr." should return the third entry.

First I created the folder where I'll work, I created a file named mostWanted.js and then, inside the file, I created an array with the names of the criminals.

// The key of the map is his actual name. 
// The value contains possible aliases the criminal has used. 
// The police are interviewing people that have been in contact with one of them. 
// They don't know if the criminal used their name or an alias.

// Write a function that returns the most probable criminal having the name (provided by interviewed people) as an input. 
// The function should return a string in the following shape: "First name: name. Aliases: alias0, alias1, aliasN". 
// If there is no match, the response should be "No match". 
// Of course, matching the actual name of the criminal is more meaningful than matching an alias, and having an exact match is more meaningful than a partial match.


var criminals = [
    ["Paul White", "Roger Night, Peter Llong Jr."], 
    ["Roger Fedexer", "Rob Ford, Pete Lord, Roger McWire"],
    ["Paul White Jr.", null],
    ["Red Fortress", "Roger Rabbit, Ross Winter"],
    ["Redford Fort", "Red Strong, Red Fort"]
];

Here we can see an array of arrays, where the first name, the first string of an array, is the name of the criminal, and the other is their aliases.

What I wanted was to iterate in this array and go through the cases, to do this I've done a for loop, and using if statements I checked the names of the criminals.

I declared a function called mostWanted, that accepts a name which is the user input. The function will search the name that the user puts if it finds a match it'll return the name matched, and if it's a name or something outside the array with the names of the criminals, will return No match.

I declared these two variables

  • criminalName = criminals[i][0] ? criminals[i][0].toLowerCase() : '';

and

  • checkAlias = criminalAliases.includes(name.toLowerCase());

These two variables helped me passing the user input to lower case using the toLowerCase javascript property.

function mostWanted(name){
   for(var i = 0; i < criminals.length; i++) {

    if(name === null) {
      return "First name: " + criminals[2][0] + " Aliases: " + criminals[2][1];
    }

    var criminalName = criminals[i][0] ? criminals[i][0].toLowerCase() : '';
    var checkName = criminalName.includes(name.toLowerCase());

    if(name === "") {
      return "No match"
    }

    if(checkName) {
        return "First name: " + criminals[i][0] + "."  + " Aliases: " + criminals[i][1];
    } 
  }

  for(var i = 0; i < criminals.length; i++) {
    var criminalAliases = criminals[i][1] ? criminals[i][1].toLowerCase() : '';
    var checkAlias = criminalAliases.includes(name.toLowerCase());

    if(name === "") {
      return "No match"
    }

    else if(name === null) {
      return "First name: " + criminals[i][0] + " Aliases: " + criminals[i][1];
    }

    if(checkAlias) {
      return "First name: " + criminals[i][0] + "."  + " Aliases: " + criminals[i][1];
    } 
  }names

  return "No match";
}

Here is the link to the full code.

Using console.log() and the name of the criminal in a string. I could see if the program was working correctly, trying with the cases that the challenge brings.

Screenshot-1.png

Testing

I learned how to use Jest, for testing cases. Jest is a JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue, and more! You can check Jest.js by clicking here

Install Jest using yarn:

yarn add --dev jest

Or npm:

npm install --save-dev jest

Finally, run yarn test or npm run test, and Jest will print a message. Passed or Failed.

Otherwise, one can call the function from the index.html console, by writing on it console.log(mostWanted("name"))

This is what the test.js file inside the main folder looks like:

const mostWanted = require('./mostWanted.js');

test('should output', () => {
    expect(mostWanted("John Paul")).toBe("No match");
});

test('should output', () => {
    expect(mostWanted("paul White")).toBe("First name: Paul White. Aliases: Roger Night, Peter Llong Jr.");
});

test('should output', () => {
    expect(mostWanted("Roger")).toBe("First name: Roger Fedexer. Aliases: Rob Ford, Pete Lord, Roger McWire");
});

test('should output', () => {
    expect(mostWanted("Ross")).toBe("First name: Red Fortress. Aliases: Roger Rabbit, Ross Winter");
});

test('should output', () => {
    expect(mostWanted("white jr")).toBe("First name: Paul White Jr.. Aliases: null");
});

test('should output', () => {
    expect(mostWanted(null)).toBe("First name: Paul White Jr. Aliases: null");
});

test('should output', () => {
    expect(mostWanted("Red")).toBe("First name: Red Fortress. Aliases: Roger Rabbit, Ross Winter");
});

test('should output', () => {
    expect(mostWanted("fedexer")).toBe("First name: Roger Fedexer. Aliases: Rob Ford, Pete Lord, Roger McWire");
});

test('should output', () => {
    expect(mostWanted("rabbit")).toBe("First name: Red Fortress. Aliases: Roger Rabbit, Ross Winter");

Screenshot-2.png

This is how I managed to solve the challenge. If you have any idea of how to improve the code or if you have a better solution, please share, I'd gladly read you!