Why does this embedded function not work (inside of a javascript algorithm), and thereby preventing the javascript algorithm from being solved?

Viewed 85

Question

Find the longest substring in alphabetical order.

Example: the longest alphabetical substring in "asdfaaaabbbbcttavvfffffdf" is "aaaabbbbctt".

There are tests with strings up to 10 000 characters long so your code will need to be efficient.

The input will only consist of lowercase characters and will be at least one letter long.

If there are multiple solutions, return the one that appears first.

My Solution

function longest(str) {
  
  //first element of count == total count of the highest number
  //second element of count == longest str so far
  let count = [0, ''];
  //temp count == length of current str
  let tempCount = [0];

//split the str to an array
let strArr = str.split('');

//loop through each letter of the string
for(let i = 0; i < strArr.length; i++){
  //if the character is higher in the alpahabet than the last then
   if(convertToNumber(strArr[i])<convertToNumber((strArr[i]-1)) || convertToNumber((strArr[i]-1))== undefined){
     tempCount[0]++;
     //if the current character is not higher than the last
   } else {
     if(tempCount[0] > count[0]){
       //change the longest str number to the length of this str
       count[0] = tempCount[0];
       //slice the new longest str
      let longestStr = strArr.slice(strArr[i]-tempCount[0], strArr[i]);
      //join the str together
      count[1] = longestStr.join('');
      //reset the temp count
      tempCount[0] = 0;
     } else {
       //reset the temp count
       tempCount[0] = 0;
     }
   }
 }
//converts the relevant letter to a code
function convertToNumber(letter){
  return letter.charCodeAt(0);
}
//returns the longest str
return count[1];
}

console.log(longest('asdfaaaabbbbcttavvfffffdf'));

The algorithm returns 'letter.charCodeAt is not a function'

Why is this the case and how can I make adjustments to fix the algorithm?

2 Answers

The problem is in here:

if(convertToNumber(strArr[i])<convertToNumber((strArr[i]-1)) || convertToNumber((strArr[i]-1))== undefined)

you are trying to subtract 1(Number) from a letter(String). It should be strArr[i-1].

But also even after fixing it you'd still get an error for:

convertToNumber(strArr[i]) < convertToNumber(strArr[i-1])

if i will be zero as strArr[-1] will give undefined.

const arr = [1, 2, 3];
console.log(arr[-1]);

Is basically undefined.charCodeAt(0);, which will produce error.

I know that the OP wants a solution for his error. fedsec showed him for this a way. So I think there is nothing else to do.

I just put here my solution of the task, so the OP can compare to other possible solutions.

function longest(str) {
    let max = 1;
    let maxStr = str.charAt(0);
    let char = maxStr;
    let testStr = maxStr;
    let count = 1;
    
    for (i=1; i<=str.length; i++) {
        let prev = char;
        char = str.charAt(i);
        if (prev <= char) {
            count++;
            testStr += char;
            if (count>max) {
                max = count;
                maxStr = testStr;
            } 
        } else {
            if ( i+count >str.length) break;
            count = 1;
            testStr = char;
        }
    }
    return(maxStr);
}

console.log(longest('asdfaaaabbbbcttavvfffffdf'));

Related