Problem 32

Run Settings
LanguageJavaScript
Language Version
Run Command
function getNumberFromArray(array) { let sum = 0; array.forEach((element, index) => { sum += element * 10 ** (array.length - 1 - index)}) ; return sum; } function checkCondition(lhsArray, rhsArray) { const preposition1 = getNumberFromArray(lhsArray.slice(0,1)) * getNumberFromArray(lhsArray.slice(1,lhsArray.length)) === getNumberFromArray(rhsArray); const preposition2 = getNumberFromArray(lhsArray.slice(0,2)) * getNumberFromArray(lhsArray.slice(2,lhsArray.length)) === getNumberFromArray(rhsArray); return preposition1 || preposition2; // return true; } // console.log(checkCondition([3,9,1,8,6], [7,2,5,4])) function checkPalindrome(toUseArray, lhsArray, rhsArray) { if (toUseArray.length === 0) { if (checkCondition(lhsArray, rhsArray)) { console.log(lhsArray + ' ' + rhsArray); } return checkCondition(lhsArray, rhsArray) } else { if (rhsArray.length === 4) { let makesUpForPalindrome = false; toUseArray.forEach((item, index) => { const arrayCopy = [...toUseArray]; const a = arrayCopy.splice(index,1) makesUpForPalindrome = makesUpForPalindrome || checkPalindrome(arrayCopy, [...lhsArray, a], rhsArray) }) const result = makesUpForPalindrome if (result) {console.log(result)} return result; } else { let sum = 0; toUseArray.forEach((item, index) => { const arrayCopy = [...toUseArray]; const a = arrayCopy.splice(index, 1) sum += checkPalindrome(arrayCopy, [], [...rhsArray,a]) ? 1 : 0 }) return sum } } } console.log(checkPalindrome([1,2,3,4,5,6,7,8,9], [], []));
Editor Settings
Theme
Key bindings
Full width
Lines