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 trialAndrei Li
34,475 PointsRepeatDetector.Scan doesn't detect repetitions. Lost, completely. Why? Everything looks just perfect.
It should be working but it's not. Please, help!
namespace Treehouse.CodeChallenges
{
class SequenceDetector
{
public virtual bool Scan(int[] sequence)
{
return true;
}
}
}
using System;
namespace Treehouse.CodeChallenges
{
class RepeatDetector : SequenceDetector
{
public override bool Scan(int[] sequence)
{
bool isSame = false;
for(int i = 0 ; i > sequence.Length; i++)
{
if(i>0) {
isSame = sequence[i] == sequence[i-1] ? true:false;
if(isSame)
{
break;
}
}
}
return isSame;
}
}
}
3 Answers
Steven Parker
231,269 PointsHere's a few hints:
- the loop should start at 1 instead of 0 (because of the reference to "i-1")
- the loop should run while the index is less than the length instead of greater
- the ternary is redundant in "
sequence[i] == sequence[i-1] ? true:false;
"
Andrei Li
34,475 PointsThank you, Steven. I managed to solve the problem. Why is ternary redundant? I think it saves some space.
Steven Parker
231,269 PointsThe conditional expression of the ternary resolves to "true" or "false". But then the rest of it substitutes "true" and "false".
So "sequence[i] == sequence[i-1] ? true:false;
" can be replaced by just "sequence[i] == sequence[i-1]
".
Andrei Li
34,475 PointsThis is a new information for me. Thank you, Steven!