1

I'm writing a function that will return true or false in regards to whether or not the input string is in alphabetical order. I'm getting undefined and not sure what I'm missing

function is_alphabetic(str) {
  let result = true;
  for (let count = 1, other_count = 2; count >= str.length - 1, other_count >= str.length; count++,
    other_count++) {
    if (str.at[count] > str.at[other_count]) {
      let result = false
    }
    return result
  }
}
console.log(is_alphabetic('abc'));
Ivar
  • 6,138
  • 12
  • 49
  • 61

5 Answers5

1

you have put return statement inside the for loop, it should be outside the loop body.

Your code is also not correct. count should start from 0, other_count should start from 1.

count >= str.length - 1 should be count < str.length - 1 (this condition is completely unnecessary in your code because other_count < str.length should be the terminating condition in the loop)

and

other_count >= str.length should be other_count < str.length

Here's your corrected code

function is_alphabetic(str) {
  let result = true;

  for (let count = 0, other_count = 1; other_count < str.length; count++, other_count++) {
      if (str[count] > str[other_count]) {
         result = false
      }
  }

  return result;
}

console.log(is_alphabetic('abc'));

Here's an alternative approach

function is_alphabetic(str){
   return str.split('')
             .every((c, idx) => str[idx + 1] ? c < str[idx + 1] : true);
}

console.log(is_alphabetic('abc'));

Keep in mind that if you want the comparisons between the characters to be case-insensitive, then convert the string in to lowercase before comparing the characters.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
0

This should do it. I used .localeCompare() as this will ignore small/capital differences and will also deal reasonably with language specific characters, like German Umlauts.

function is_alphabetic(str){
  return !str.split('').some((v,i,a)=>i&&v.localeCompare(a[i-1])<0)
}
['abcdefg','aacccRt','ashhe','xyz','aüv'].forEach(s=> console.log(s,is_alphabetic(s)) );
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

I think is easier if you compare the string using this function:

var sortAlphabets = function(text) {
    return text.split('').sort().join('');
};

This produces results like:

sortAlphabets("abghi")
output: "abghi"

Or:

sortAlphabets("ibvjpqjk")
output: "bijjkpqv"

if you want to know if your string is alphabetically sorted, you might use:

var myString = "abcezxy"
sortAlphabets(myString) == myString
output: false

Or in case, you want to create an specific function:

function isSorted(myString) {
    return sortAlphabets(myString) == myString
}

And for that case, you can use:

isSorted("abc")

var sortAlphabets = function(text) {
        return text.split('').sort().join('');
    };

function isSorted(myString) {
        return sortAlphabets(myString) == myString
    }
    
 alert("is abc sorted: " + isSorted("abc"));
  alert("is axb sorted: " + isSorted("axb"));
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0

There are two issues in your code:

  1. Your return statement is in your for-loop. To avoid such mistakes you can get a code-formatter like prettier;
  2. Your for-loop condition is invalid. Keep in mind that the second part of the for-loop statement is supposed to be true to do an iteration and false to stop doing iterations. In this case, your condition count >= str.length-1, other_count >= str.length will first evaluate count >= str.length-1, discard the result because of the comma operator, evaluate other_count >= str.length which immediately resolves to false.

These two things together make it so your function never returns, which the javascript runtime interprets as undefined.

Hope this helps you understand what went wrong. But like many other pointed out, there are better ways to tackle the problem you're trying to solve.

0

You just have to compare the string with its corresponding 'sorted' one

let string = 'abc'.split('').join('');
let sortedString = 'abc'.split('').sort().join('');

console.log(sortedString === sortedString)

let string2 = 'dbc'.split('').join('');
let sortedString2 = 'dbc'.split('').sort().join('');

console.log(string2 === sortedString2)
ABGR
  • 4,631
  • 4
  • 27
  • 49