Find Needle in Haystack with JavaScript

Day 19 / 30 – JavaScript Coding Challenge Problem: Given two strings, needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if it doesn’t exist. Solution: var strStr = function (haystack, needle) {     if (needle.length === 0) return -1     for (let i = 0; i <= haystack.length - needle.length; i++) {         let found = true;         for (let j = 0; j < needle.length; j++) {             if (haystack[i + j] !== needle[j]) {                 found = false;                 break;             }         }         if (found) {             return i         }     }     return -1; }; #JavaScript #DSA #CodingPractice #100DaysOfCode #ProblemSolving #FrontendDevelopment

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories