The integers can appear anywhere in the word, but they cannot be all adjacent to each other. If every word contains exactly 3 unique integers somewhere within it, then return the string true, otherwise return the string false.
function StringChallenge(str) {
const words = str.split(” “);
for (let word of words) {
const digits = word.match(/d/g);
if (!digits || digits.length !== 3) return "false";
// Check for adjacent digits
for (let i = 0; i < digits.length - 1; i++) {
if (digits[i] === digits[i + 1]) return "false";
}
// Check for unique digits (alternative approach)
const digitCount = {};
for (let digit of digits) {
digitCount[digit] = (digitCount[digit] || 0) + 1;
if (digitCount[digit] > 1) return "false";
}
}
return "true"; i tried thiss and ended up getting both false outputs
}
Victor Kamau is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.