JSFiddle - React, Tailwind, and code Playground

by Artem

JavaScript

'use strict';

function isMerge(s, part1, part2) {
	if (!s || !part1 || !part2) return false;
  let tmpS = s.split('');
  let tmpP1 = part1.split('');
  let tmpP2 = part2.split('');
  let success = true;
  console.log('string: ', s.replace(/ /g, '_'), ' part1: ', part1.replace(/ /g, '_'), ' part2: ', part2.replace(/ /g, '_'));
  if (tmpP1.length + tmpP2.length !== tmpS.length) return false;
  tmpS.reduce((obj, letter, idx) => {
    let res1 = tmpP1.findIndex((l) => l === letter);
    let res2 = tmpP2.findIndex((l) => l === letter);
    console.log('index: ', idx, ' res1: ', res1, 'res2: ', res2);
    if (res1 === obj.str1) {
      obj.str1++;
    } else if (res2 === obj.str2) {
      obj.str2++;
    } else {
      success = false;
    }
    return obj;
  }, {
    str1: 0,
    str2: 0
  });
  return success;
}

console.log(isMerge('codewars', 'code'));