Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jackson Monk
4,527 PointsDon't understand why this doesn't work
I have spent a good 4 hours trying to figure out how to detect what the first repeated character in a string is, and I have code, but my brain is shot. I know it has something to do with the length of charArr, but thinking about all those loops and the length of charArr is overloading my brain. Can someone help me please? Here is my code. I probably overcomplicated it like I usually do.
window.onload = function() {
let input = document.getElementsByTagName('input')[0];
let button = document.getElementsByTagName('button')[0];
let index;
let characters;
let splitChar;
let charArr = [null, null];
button.addEventListener('click', () => {
characters = input.value;
splitChar = characters.split('');
index = charArr.indexOf(1);
looping:
for (let i = 0; i < splitChar.length; i ++) {
if (i === 0) {
for (let j = 0; j < charArr.length - 1; j ++) {
if (splitChar[i] == charArr[j]) {
document.write('The first repeated character in your sentence was: ' + splitChar[i]);
break looping;
}
else {
charArr.push(splitChar[i]);
if (splitChar[i + 1] == splitChar.length) {
document.write('You never had a repeating character!');
}
}
if (j === 0) {
charArr.splice(index, 1);
}
}
}
else {
for (let j = 0; j < charArr.length; j ++) {
if (splitChar[i] == charArr[j]) {
document.write('The first repeated character in your sentence was: ' + splitChar[i]);
break looping;
}
else {
charArr.push(splitChar[i]);
if (splitChar[i + 1] == splitChar.length) {
document.write('You never had a repeating character!');
}
}
if (j === 0) {
charArr.splice(index, 1);
}
}
}
}
});
};
Here is html too:
<html>
<head></head>
<body>
<h1>First Recurring Character</h1>
<h3>Type a series of characters in the box below and the result will be the first character that occurs twice in your sentence</h3>
<input type = text> <button>Enter</button>
<script src = floucore.js></script>
</body>
</html>
Help is appreciated, thanks.
1 Answer
Steven Parker
243,656 PointsI haven't analyzed your program, but I'll suggest a solution strategy:
The "indexOf()" value of any unique letter will be the same as its position in the string. But the value of a repeated letter will be less than its own position index.
Jamie Carter
Front End Web Development Techdegree Student 12,096 PointsJamie Carter
Front End Web Development Techdegree Student 12,096 PointsAre you trying to find the 1st letter (eg E) that appears twice in a sentence?