First line of the input contains an integer, N. Then N lines follow.
From the second line onwards, each line contains a set of W words separated by a single space.
For every line,
Print (console.log()) 1 if the conversation starts with hackerrank
Print 2 if the conversation ends with hackerrank
Print 0 if the conversation starts and ends with hackerrank
Print -1 if none of the above.
Code (JavaScript)
const processData = (input) =>
input
.split("\n")
.slice(1)
.map((line) => line.match(/^(hackerrank)\b.*(?<!\bhackerrank)$/g) ? console.log(1) : line.match(/ hackerrank$/g) ? console.log(2) : line.match(/^(hackerrank)\b(.*(?<!\bhackerrank))?$/g) ? console.log(0) : console.log(-1));
Input
4
i love hackerrank
hackerrank is an awesome place for programmers
hackerrank
i think hackerrank is a great place to hangout
Expected output
2 1 0 -1
(one number by line… im having a hard time with my markdown)
The output I get
2 1 0 -1 -1
(one number by line… im having a hard time with my md)
what’s going on with this last -1?
i’ve tested this code on codepen and it works but it doesn’t work on hackerrank
thank you
>Solution :
You can remove empty lines before parsing with filter.
input.split("\n").slice(1).filter(Boolean)
Alternatively, remove leading and trailing whitespace from the input with String#trim before splitting.
input.trim().split("\n")