peterschussheim
11/2/2016 - 1:23 PM

toy-problem-2

toy-problem-2

const firstNonRepeatingChar = (s) => {
    var result = false;
    while(s) {
        var l = s.length,
            c = s.charAt(0),
            regex = new RegExp(c, "gi");
        
        s = s.replace(regex, "");
        if (s.length === l - 1) { ///if  string.length === l(ref) - 1
            result = c;
            break;
        } else {
            return "sorry";
        }
    }
    return result;
}

const bird = firstNonRepeatingChar('Uber for horses');
// const baba = nonRepeatingChar("BABA");
// const ala = nonRepeatingChar("ALAMABA");
// const xx = nonRepeatingChar("XXXXXXX");
///For this algorithm problem (non-repeated character),\\our goal is to return the first non-repeating character\\(given an arbitrary input string).\\\\For strings with all repeats, we will return a default word\\(in this case ‘sorry’).\\\\Example inputs/outputs:\\\\input: “ABCDBIRDUP”\\output: “A”\\\\input: “ALAMABA”\\output: “L”\\\\input: “XXXXXXX”\\output: “sorry”\\\\input: “BABA”\\output: “sorry”

// nonRepeatingChar :: String -> String

function nonRepeatingChar(str){
    let foundRepeat = false;
    while(result) {
        
        if(charCount[str[i]]){
            charCount[str[i]] = 'More Than One';
        } else {
            charCount[str[i]] = 'One Time';
        }
    }    
    for(var j = 0; j < string.length; j++){
        if(charCount[str[j]] === 'One Time'){
          return str.charAt(j);      
        }
    }
    
    return "sorry";
}

// nonRepeatingChar('ABCDBIRDUP');
nonRepeatingChar("BABA")
// nonRepeatingChar("ALAMABA")
// nonRepeatingChar("XXXXXXX")

nonRepeatingChar("ABCDBIRDUP")

toy-problem-2

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.