const testData = [{
args: [
['super', 'bow', 'bowl', 'tar', 'get', 'book', 'let'], 'superbowl'
],
result: ['super', 'bowl', [0, 2]],
},
{
args: [
['bow', 'crystal', 'organic', 'ally', 'rain', 'line'], 'rainbow'
],
result: ['bow', 'rain', [4, 0]],
},
{
args: [
['top', 'main', 'tree', 'ally', 'fin', 'line'], 'treefinally'
],
result: null,
},
{
args: [
['get', 'bowl', 'folk', 'wind', 'rain', 'moon', 'post', 'bed', 'land', 'fly', 'silver', 'less', 'red', 'lit', 'main', 'spell', 'tar', 'let', 'rest', 'super', 'top', 'deck', 'quick', 'lore', 'year', 'fire', 'guile', 'crystal', 'be', 'light', 'time', 'love', 'grab', 'low', 'dust', 'dazzle', 'tail', 'bow', 'fairy', 'fall', 'book', 'free', 'back', 'tale', 'door', 'fort', 'bel', 'in', 'shear', 'line', 'get', 'bowl', 'folk', 'wind', 'rain', 'moon', 'post', 'bed', 'land', 'fly', 'silver', 'less', 'red', 'lit', 'main', 'spell', 'tar', 'let', 'rest', 'super', 'top', 'deck', 'quick', 'lore', 'year', 'fire', 'guile', 'crystal', 'be', 'light', 'time', 'love', 'grab', 'low', 'dust', 'dazzle', 'tail', 'bow', 'fairy', 'fall', 'book', 'free', 'back', 'tale', 'door', 'fort', 'bel', 'in', 'shear', 'line'], 'beknight'
],
result: null,
},
];
const isArraysEqual = (firstArr, secondArr) => {
return firstArr.every((item, index) => {
return Array.isArray(item) ? isArraysEqual(item, secondArr[index]) : item === secondArr[index];
});
};
const runTests = (cb) => {
testData.forEach((item, index) => {
const result = cb(...item.args);
if (item.result === null) {
result === null ? console.log(`Test ${index + 1} passed`) : new Error(`Test ${index + 1} failed: expected null, instead got ${result}`);
} else {
isArraysEqual(result, item.result) ? console.log(`Test ${index + 1} passed`) : console.error(`Test ${index + 1} failed: expected ${item.result}, instead got ${result}`);
}
});
};
function locateWordPairs(arr, str) {
var len = arr.length;
for (let i = 0; i < len; i++) {
var outerStr = arr[i];
for (let j = 0; j < len; j++) {
if (outerStr.concat(arr[j]) === str) {
return [arr[Math.min(i,j)], arr[Math.max(i,j)],[i, j]];
}
};
};
return null
};
runTests(locateWordPairs);
function getStringParts(words, str) {
const pairs = [];
for (let i = 0; i < words.length; i++) {
const word = words[i];
const index = str.indexOf(word);
if (index < 0) continue;
const pair = index ? str.substring(0, index) : str.substr(word.length);
if (pairs.indexOf(word) >= 0) {
return [pair, word, index ? [words.indexOf(pair), i] : [i, words.indexOf(pair)]];
}
pairs.push(pair);
}
return null;
};
runTests(getStringParts);
function fn(array, word) {
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] + array[j] === word) {
return [array[i], array[j], [i, j]];
} else if (array[j] + array[i] === word) {
return [array[i], array[j], [j, i]]
}
}
}
return null
};
runTests(fn);
const findPair = (words, target) => {
for (let i = 1; i < target.length - 1; i++) {
const firstWord = target.slice(0, i);
const secondWord = target.slice(i);
const firstIndex = words.indexOf(firstWord);
const secondIndex = words.indexOf(secondWord);
if (firstIndex !== -1 && secondIndex !== -1) {
return firstIndex > secondIndex ? [secondWord, firstWord, [firstIndex, secondIndex]] : [firstWord, secondWord, [firstIndex, secondIndex]];
}
}
return null;
};
runTests(findPair);
const findPair = (words, target) => {
const getUniq = (arr) => {
const seen = {};
const output = [];
let j = 0;
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (seen[item] !== 1) {
seen[item] = 1;
output[j++] = item;
}
}
return output;
};
const uniq = getUniq(words).sort((a, b) => ((target.match(a) && target.match(a).index) || 0) - ((target.match(b) && target.match(b).index) || 0));
if (uniq.length >= 2) {
const result = [];
for (let i = 0; i < uniq.length - 1; i++) {
for (let j = i + 1; j < uniq.length; j++) {
if (`${uniq[i]}${uniq[j]}` === target) {
result.push(uniq[i], uniq[j]);
break;
}
}
}
if (!result.length) {
return null;
}
const sorted = result.slice().sort((a, b) => words.indexOf(a) - words.indexOf(b));
return [...sorted, [words.indexOf(result[0]), words.indexOf(result[1])]];
}
return null;
};
runTests(findPair);