Sat May 29 2021
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
const foo = function(string) {
  if (!string) return
   // If string is empty, return 0
    if (!string) return 0
  
    // Holds our count of highest substring length
    let counter = 0;  
  

    // Loop  will add elements here
    let tmpArray = []
  
    const arr = string.split("")
    arr.forEach((e, i) => {
        // If this character already exists in tmpArray...
        if (tmpArray.indexOf(e) != -1) {
            // Update counter if tmpArray's length if higher
            if (tmpArray.length > counter) {
                counter = tmpArray.length
            }
            
            // Clear tmpArray with the current element in it
            tmpArray = [e]
            return
        }
        
        // Else, if tmpArray does not have this character in it, add it
        tmpArray.push(e)
        console.log(tmpArray)
    })
    
    if (tmpArray.length > counter) {
      counter = tmpArray.length
    }
  
  return counter
};

const r = foo("dvdf")
console.log(r)

/*
[a]
[ab]
[abc]
theres an a.set counter to tmp.length if temp.length > counter
clear tmp array
*/