issue with substring indexing

DBN

Instructions for this kata:

In this Kata, we will check if a string contains consecutive letters as they appear in the English alphabet and if each letter occurs only once.

It seems that my code is indexing the strings differently per function call on this one. for example, on the first test "abcd", the starting index is shown as 0, which is correct, and on the second example, "himjlk", the

 var subString = alphabet.substring(startIndex, length);

returns "g", instead of "h"

troubleshooting this section

var length = orderedString.length;
  //startChar for string comparison
  var startChar = orderedString.charAt(0);
  //find index in aphabet of first character in orderedString.
  var startIndex = alphabet.indexOf(startChar);
  //create substring of alphabet with start index of orderedString and //orderedString.length
  var subString = alphabet.substring(startIndex, length);

function solve(s) {
  //alphabet string to check against
  const alphabet = `abcdefghijklmnopqrstuvwxyz`;
  //check s against alphabet
  //empty array to order input string
  var ordered = [];
  //iterate through alphabet, checking against s
  //and reorder input string to be alphabetized
  for (var z in alphabet) {
    var charToCheck = alphabet[z];
    for (var i in s) {
      if (charToCheck === s[i]) {
        ordered.push(s[i]);
      }
      //break out of loop if lengths are the same
      if (ordered.length === s.length) {
        break;
      }
    }
    if (ordered.length === s.length) {
      break;
    }
  }
  //join array back into string
  var orderedString = ordered.join(``);
  //length for future alphabet substring for comparison
  var length = orderedString.length;
  //startChar for string comparison
  var startChar = orderedString.charAt(0);
  //find index in aphabet of first character in orderedString.
  var startIndex = alphabet.indexOf(startChar);
  //create substring of alphabet with start index of orderedString and orderedString.length
  var subString = alphabet.substring(startIndex, length);

  //return if the two are a match
  return subString == orderedString ? true : false;
}

console.log(solve("abdc")); //expected `true`
console.log(solve("himjlk")); // expected `true`

console.log(solve("abdc")); should provide the substring "abcd" and return true, which it does.

console.log(solve("himjlk")); should put together "hijklm" and return true, but instead gives me g based on index 6 of alphabet, not sure why it's doing this, should be index 7 "h" returns false based upon this error.

obscure

The problem is that you're using substring() instead of substr(). Though that might sound similar there's a difference.

With substring the second parameter doesn't determine the length as you might have expected. It's actually the index to stop. That your function works as expected with the string abcd is pure coincidence since in this case the length from index 0 and the end index are the same.

function solve(s){
  const alphabet = `abcdefghijklmnopqrstuvwxyz`;
  var ordered = [];
  for(var z in alphabet){
    var charToCheck = alphabet[z];
    for(var i in s){
      if(charToCheck === s[i]){
        ordered.push(s[i]); 
      }
      if(ordered.length === s.length){ break; }
    }
    if(ordered.length === s.length){ break; }
  }
  var orderedString = ordered.join(``);
  var length = orderedString.length;
  var startChar = orderedString.charAt(0);
  var startIndex = alphabet.indexOf(startChar);
  var subString = alphabet.substr(startIndex, length);

  return subString == orderedString ? true: false;
}

console.log(solve("himjlk"));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事