If you missed the introductory post, it's here. For a list of previously solved katas, please refer to the bottom of this page.
If this is your first time seeing my post, please note - these katas are probably randomly assigned per user so please don't go into Codewars thinking we will have exactly the same user experience, I am just posting these in the order that I got them.
Kata #7
DESCRIPTION:
Can you find the needle in the haystack?
Write a function findNeedle() that takes an array full of junk but containing one "needle"
After your function finds the needle it should return a message (as a string) that says:
"found the needle at position " plus the index it found the needle, so:
Example(Input --> Output)
["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5"
Starting code:
function findNeedle(haystack) {
// your code here
}
My attempt:
function findNeedle(haystack) {
// your code here
for (i = 0 ; i < haystack.length ; i++){
if(haystack[i]==="needle"){
return "found the needle at position " + i
}
}
}
Fair warning folks, I thought the solutions page would just be like the challenges I've done before but I've been wrong before so strap in because it's going to get wild. Ranked #1 in 'Best Practice' with 944 votes and 298 'Clever' votes at the time of writing is this:
function findNeedle(haystack) {
return "found the needle at position " + haystack.indexOf("needle");
}
Coming in at rank #2 which shooketh me goes to this one with 325 votes and 1120 'Clever' votes at the time of writing:
var a = "a";
var b = "b";
var c = "c";
var d = "d";
var e = "e";
var f = "f";
var g = "g";
var h = "h";
var i = "i";
var j = "j";
var k = "k";
var l = "l";
var m = "m";
var n = "n";
var o = "o";
var p = "p";
var q = "q";
var r = "r";
var s = "s";
var t = "t";
var u = "u";
var v = "v";
var w = "w";
var x = "x";
var y = "y";
var z = "z";
var space = " ";
var theWordNeedleWhichWillNeedToBeFoundWithinTheHaystackContainingTheNeedle = [].concat(n).concat(e).concat(e).concat(d).concat(l).concat(e).join('');
var returnStringPrefix = [].concat(f).concat(o).concat(u).concat(n).concat(d).concat(space).concat(t).concat(h).concat(e).concat(space).concat(n).concat(e).concat(e).concat(d).concat(l).concat(e).concat(space).concat(a).concat(t).concat(space).concat(p).concat(o).concat(s).concat(i).concat(t).concat(i).concat(o).concat(n).concat(space).join('');
function findNeedle ( theHaystackContainingTheNeedle )
{
var indexOfNeedleWithinTheHaystackContainingTheNeedle = -1;
var numberOfStrawsPlusTheNeedleThatAreContainedWithinTheGiantHaystackContainingTheNeedleWhichIsProbablyLocatedInAFieldSomewhereNearABarnOnAFarmPotentiallyWithinTheVicinityOfRuralTexas = getLengthOfArrayContainingElements( theHaystackContainingTheNeedle );
for(
var currentIndexOfTheCurrentlyExecutingForLoop = 0;
currentIndexOfTheCurrentlyExecutingForLoop <= numberOfStrawsPlusTheNeedleThatAreContainedWithinTheGiantHaystackContainingTheNeedleWhichIsProbablyLocatedInAFieldSomewhereNearABarnOnAFarmPotentiallyWithinTheVicinityOfRuralTexas;
currentIndexOfTheCurrentlyExecutingForLoop = currentIndexOfTheCurrentlyExecutingForLoop + 1
)
{
if(currentIndexOfTheCurrentlyExecutingForLoop < 0)
{
throw new Error("This shouldn\"t happen".replace("\u0022","\u2019"));
}
if(currentIndexOfTheCurrentlyExecutingForLoop > numberOfStrawsPlusTheNeedleThatAreContainedWithinTheGiantHaystackContainingTheNeedleWhichIsProbablyLocatedInAFieldSomewhereNearABarnOnAFarmPotentiallyWithinTheVicinityOfRuralTexas)
{
throw new Error("This shouldn\"t happen".replace("\u0022","\u2019"));
}
var theValueOfTheCurrentStrawOrNeedleThatResidesWithinTheLargeHaystack = getElementFromArrayGivenIndex( theHaystackContainingTheNeedle, currentIndexOfTheCurrentlyExecutingForLoop );
if( theValueOfTheCurrentStrawOrNeedleThatResidesWithinTheLargeHaystack === theWordNeedleWhichWillNeedToBeFoundWithinTheHaystackContainingTheNeedle) {
indexOfNeedleWithinTheHaystackContainingTheNeedle = currentIndexOfTheCurrentlyExecutingForLoop;
}
}
var theCompleteReturnValueContainingTheReturnPrefixAndTheIndexOfTheNeedleWithinTheHaystackContainingTheNeedle = returnStringPrefix.split('').concat(indexOfNeedleWithinTheHaystackContainingTheNeedle).join('');
return theCompleteReturnValueContainingTheReturnPrefixAndTheIndexOfTheNeedleWithinTheHaystackContainingTheNeedle;
}
function getElementFromArrayGivenIndex( arrayContainingElements, indexToRetrieveFromArrayContainingElements )
{
var valueFromArrayWhereIndexWasBeingSearchedFor = -1;
var numberOfElementsInArrayContainingElements = getLengthOfArrayContainingElements(arrayContainingElements);
for(
var currentIndexOfTheCurrentlyExecutingForLoop = 0;
currentIndexOfTheCurrentlyExecutingForLoop <= numberOfElementsInArrayContainingElements;
currentIndexOfTheCurrentlyExecutingForLoop = currentIndexOfTheCurrentlyExecutingForLoop + 1
)
{
if(currentIndexOfTheCurrentlyExecutingForLoop < 0)
{
throw new Error("This shouldn\"t happen".replace("\u0022","\u2019"));
}
if(currentIndexOfTheCurrentlyExecutingForLoop > numberOfElementsInArrayContainingElements)
{
throw new Error("This shouldn\"t happen".replace("\u0022","\u2019"));
}
var valueBasedOnCurrentIndexOfTheArrayThatIsCurrentlyBeingReferencedWithinTheExecutingForLoop = arrayContainingElements[ currentIndexOfTheCurrentlyExecutingForLoop ];
if(currentIndexOfTheCurrentlyExecutingForLoop === indexToRetrieveFromArrayContainingElements)
{
valueFromArrayWhereIndexWasBeingSearchedFor = valueBasedOnCurrentIndexOfTheArrayThatIsCurrentlyBeingReferencedWithinTheExecutingForLoop;
}
}
return valueFromArrayWhereIndexWasBeingSearchedFor;
}
function getIndexFromArrayGivenValue( arrayContainingElements, valueForWhichToUseToFindTheIndexAndRetrieveItFromArrayContainingElements )
{
var indexFromArrayWhereValueWasFound = -1;
var numberOfElementsInArrayContainingElements = getLengthOfArrayContainingElements(arrayContainingElements);
for(
var currentIndexOfTheCurrentlyExecutingForLoop = 0;
currentIndexOfTheCurrentlyExecutingForLoop <= numberOfElementsInArrayContainingElements;
currentIndexOfTheCurrentlyExecutingForLoop = currentIndexOfTheCurrentlyExecutingForLoop + 1
)
{
if(indexFromArrayWhereValueWasFound > -1)
{
continue;
}
var valueBasedOnCurrentIndexOfTheArrayThatIsCurrentlyBeingReferencedWithinTheExecutingForLoop = getElementFromArrayGivenIndex( arrayContainingElements, currentIndexOfTheCurrentlyExecutingForLoop );
if( valueBasedOnCurrentIndexOfTheArrayThatIsCurrentlyBeingReferencedWithinTheExecutingForLoop === valueForWhichToUseToFindTheIndexAndRetrieveItFromArrayContainingElements )
{
indexFromArrayWhereValueWasFound = currentIndexOfTheCurrentlyExecutingForLoop;
}
}
return indexFromArrayWhereValueWasFound;
}
function getLengthOfArrayContainingElements( arrayContainingElementsForWhichToRetreiveTheLengthFrom )
{
var theLengthOfTheArrayContainingElementsForWhichToRetreiveTheLengthFrom = arrayContainingElementsForWhichToRetreiveTheLengthFrom.length;
if(theLengthOfTheArrayContainingElementsForWhichToRetreiveTheLengthFrom < 0)
{
theLengthOfTheArrayContainingElementsForWhichToRetreiveTheLengthFrom = 0;
}
return theLengthOfTheArrayContainingElementsForWhichToRetreiveTheLengthFrom;
}
Insane trolling right here. It's like sending a text message to someone but in morse code in this day and age. It seems this was done with the objective of trying to write out the longest solution, so I guess they achieved that and are very pleased with themselves now with all the votes that they got. Maybe the 'Clever' votes in this case were meant like a "haha very funny" way. So I guess a shoutout is in order for these four persons who are the first comedians I've encountered on this platform:
![]()
(Swivelgames, jy1138, Kasterborous_18, AlaZrafi's profile links)
Trying to take a mental note not to take any solutions seen from these users too seriously.
Learned the .indexOf method from rank #1 but rank #2 just blows this specific challenge out of the water.
That's it for Kata #7, stay tuned for more katas to be solved!
Link to Kata #1: Square(n) Sum
Link to Kata #2: Convert a Number to a String
Link to Kata #3: DNA to RNA Conversion
Link to Kata #4: Remove First and Last Character