Need some Java help please

All, I’ll be honest- I’m a project manager interviewing for a new job. They’re looking for someone with “a little bit” of a technical background. I am not familiar with JavaScript and was hoping someone could could help with the below situation.

Your colleague has pointed out an issue and is asking for your help to understand what’s going on. They want to set a flag to true if the patient has an insurance that includes the word “Blue” – they thought this would be easy but are seeing some unexpected results. Using the sample inputs below, please troubleshoot the issue, provide a brief explanation of your findings, and suggest how the code should be updated.

function isBlueInsurance(inputMessage) {
 
​if (inputMessage.indexOf('Blue') != -1) {
 
​return true;
​
​} else {
 
​return false;
 
​}
 
}
Sample inputs (format → ‘Patient ID|Patient Name|Age|Visit ID|Diagnosis|Unit|Room|Insurance’)
 
message1 = ‘123|Steven Martin|68|1111|Heart failure|5W|18A|Medicare’;
message2 = ‘456|Jennifer Craig|43|1112|Gastric bypass surgery|T8|02B|Blue Shield’;
message3 = ‘789|Lisa Blueser|15|1113|Pneumonia|E3|13A|Self Pay’;

what kind of help are you hoping for? how much do you understand of the challenge?

Java or JavaScript? they are not the same thing


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

So let’s help you do that. Do you understand what the code is currently doing? If so, can you explain to us why it is buggy?

We don’t need perfect answers here. Just trying to establish how much you know about coding in general. If you are familiar with any other programming languages then you might be able to get the gist of what is going on here.

And just to reiterate, Java and JavaScript are not the same language and really don’t have anything to do with each other, except that they both descend from the language C so they share some syntax in common.

But yeah, how is your JS?

Do you understand what (inputMessage.indexOf('Blue') != -1) is doing? You might want to look here.

Once you understand what that is doing, take a look closely at how that function relates to the data.

Thanks - I do not have a coding background - I’m a nurse. I’m a project manager that’s worked for two healthcare software companies. This is the first time I’ve ever been asked to interpret code. I have some small experience with SQL reports

If the patient has insurance named with a “blue” then it should return as “true”

The other two messages should be false

One of the patient names has “Blue” in it. So indexOf will match on it.

My first response would be not to use a string for the data. Otherwise, you will have to come up with a solution that handles the fact that the string “Blue” can appear in other places in the string.

Thanks @Hogwild, this helps a lot. It sounds like this company does not have the same definition of project manager that the other two healthcare companies you’ve worked for have :slight_smile: We can try our best to help you understand what is going on with the code they gave you but without any programming background I’m not sure how far we will get. Fortunately, this example is really very simple, but it does require a knowledge of a few JS String methods to understand what the problem is and how to solve it.

The function is being passed a string that is comprised of several fields (delimited by the bar [or pipe] character) containing various data about the patient. The last field in the string is the insurance information. The String method indexOf looks through the entire string to find the first instance of what it is looking for. In this case, it will look through the entire string for the word ‘Blue’. If indexOf finds ‘Blue’ it will return the index in the string where it was found and the function will return true. Otherwise it will return -1 and the function will return false.

Looking at the three “message” strings being passed into the function, message1 does not have the word ‘Blue’ in it so indexOf will return -1 and the function will return false (which is what we want). message2 does have the word ‘Blue’ in it so indexOf will return the numerical spot in the string where it occurs and the function will return true (which is also what we want). But message3 also has the word ‘Blue’ in it, so indexOf will not return -1 in the case and the function will return true even though the message3 patient does not have insurance with the word ‘Blue’ in it. Hopefully you can see how using indexOf to search for ‘Blue’ in the entire string can give you an unexpected and wrong result.

So how do you solve this? Notice that each message has the same format, which is very convenient in this case. There is a String method which allows you to split a string into an array based on a delimiter. So we can split each string passed into the function into an array, so that we have access to each field in the string individually, and then use indexOf('Blue') only on the last field in the string, ensuring that we only return true for patients that have ‘Blue’ in their insurance.

Do they want you to actually write the code you would use to fix this?

Thanks - I pasted the scenario above. To your point- it’s looking for the word Blue but it’s not necessarily looking for it in the correct field. It’s looking at all fields. Sound correct?

Yes, you could say it is looking at all fields. I think it might be a little more accurate to say that the indexOf method has no concept of fields at all, it just searches through the string, starting at the beginning, trying to find what you asked it to search for.

The solution here (or at least one solution) is to convert the string into actual separate fields and then only search on the insurance field.

In a real world application, this function would most likely not get a string with delimited fields like this. This is sort of a contrived example.

Thanks - I think I have enough information.

Thanks again

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.