Dan Rubio Dan Rubio - 1 year ago 72
Javascript Question

Can someone help explain why I get this 'Value is not what was expected' error with Javascript?

I'm working on beefing up my Javascript skills with some katas on codewars. Here is one such kata:

At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.

The restriction is that the characters in part1 and part2 are in the same order as in s.

The interviewer gives you the following example and tells you to figure out the rest from the given test cases.

For example:

'codewars' is a merge from 'cdw' and 'oears':

s: c o d e w a r s = codewars
part1: c d w = cdw
part2: o e a r s = oears

Here is my solution that I am working on:

function presentInString(element, index, array) {
return string.includes(element);

function isMerge(s, part1, part2) {
string = s;
var mergedParts = (part1 + part2).split('');

My approach is simple, I get passed in a string
and with parts
The above methods should return true because all of the characters are in the string. but I keep getting a
Value is not what was expected
error. I must be using the
method wrong but I'm not sure how. I pretty much based it off of the Javascript MDN docs. Could someone pinpoint what I'm doing wrong?

  • It's a kata in progress by the way and I haven't tested all edge cases. Some cases will fail.

Additionally, do I have to create another function to pass into
? I would rather just right the logic in the scope of
instead of writing another function to pass off to.

Answer Source

I can't vouch for your overall algorithm, but the problem with isMerge is that it doesn't use the return value of every and doesn't return anything. You probably wanted:

return mergedParts.every(presentInString);

as the last line, which makes the return value of isMerge whatever every returns (true if presentInString returns a truthy value for each element, false if it doesn't).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download