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

MIke Legemah
Front End Web Development Techdegree Student 13,732 PointsHow to write a javascript function that checks if first and last characters in a string are equal
I been doing this javascript challenge and I'm pretty close, but something is off.Here's the challenge:
Given an array of strings containing three types of braces: round (), square [] and curly {} Your task is to write a function that checks whether the braces in each string are correctly matched. Prints 1 to standard output (console.log) if the braces in each string are matched and 0 if they're not (one result per line)
my code is this:
var infoToParse = [ ")(){}", "[]({})", "([])", "{()[]}", "([)]" ];
function checkBraces(infoToParse) {
var tabChars = infoToParse;
for (i= 0; tabChars.length - 1; i+=1) {
if (tabChars[i].charAt(0) === tabChars[i].charAt(tabChars[i].length-1)){
console.log(1);
}else{
console.log(0);
}
}
}
checkBraces(infoToParse);
The output with the current array items should be Output: 0 1 1 1 0
1 Answer

LaVaughn Haynes
12,397 Pointsyour code is testing whether the first and last character in the string are the same. In all cases they are not [ ")}", "[)", "()", "{}", "(]"] so your code is actually working correctly. It's just not solving the problem that you are trying to solve.
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 Pointsto get yours to work I made a few modifications. This makes sure the first and last characters match. It does NOT make sure that all pairs are "correctly matched"
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 PointsI was thinking about your goal though and I made this script that I think is doing what you are trying to do in making sure they are "correctly matched". This is OK: { () } and this is not OK: [(])