function checkThirdNumber() {
let num1 = parseInt(document.querySelectorAll('.checkThirdInput')[0].value);
let num2 = parseInt(document.querySelectorAll('.checkThirdInput')[1].value);
let num3 = parseInt(document.querySelectorAll('.checkThirdInput')[2].value);
document.getElementById('checkThirdResult').innerHTML = findThirdNumber([num1, num2, num3]);
}
function findThirdNumber(numbers) {
let twoEquals = [];
let oneUnequal = [];
for (var i = 0; i < numbers.length; i++) {
for (var j = i + 1; j < numbers.length; j++) {
if (numbers[i] == numbers[j]) {
twoEquals.push(numbers[i]);
}
if (numbers[i] != numbers[j]) {
oneUnequal.push(numbers[i]);
}
}
}
if ((twoEquals.length == 2) && (oneUnequal.length == 1)) {
return 'The two equal numbers are: ' + twoEquals + '<br>' + 'The third number is: ' + oneUnequal;
} else {
return 'Numbers do not match.'
}
}
<input type="number" class="checkThirdInput" value="3">
<input type="number" class="checkThirdInput" value="3">
<input type="number" class="checkThirdInput" value="4">
<button onclick="checkThirdNumber()">Check</button>
<div id="checkThirdResult"></div>
I attempted to resolve the issue using the code above, but for some reason it is not functioning correctly. The algorithm checks for two matching numbers and one different number in the input array, but there are some errors in the implementation.