본문 바로가기

개발/jQuery

수학 문제 1-9까기 숫자를 한번만 사용해서, 3자리 * 2자리 = 4자리 결과가 나오도록

function getNumbersList(elements, count) { var a; var numbersList = []; --count; for (a = 0; a < elements.length; a++) { if (count == 0) { numbersList.push(elements[a]); } else { var dupplicatedElements = elements.slice(); var first = dupplicatedElements.splice(a, 1); var recursiveList = getNumbersList(dupplicatedElements, count); for (b = 0; b < recursiveList.length; b++) { numbersList.push(first.toString() + recursiveList[b].toString()); } } } return numbersList; } var elements = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var numbersList = getNumbersList(elements, 9); var answerList = []; for (i = 0; i < numbersList.length; i++) { var numbers = numbersList[i]; abc = parseInt(numbers.substring(0, 3)); de = parseInt(numbers.substring(3, 5)); fghi = parseInt(numbers.substring(5, 9)); if (abc * de == fghi) { answerList.push(numbers); } } for (i = 0; i < answerList.length; i++) { document.querySelector("#test_result").innerHTML += answerList[i] + "
"; }