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'));
you have put
returnstatement inside the for loop, it should be outside the loop body.Your code is also not correct.
countshould start from 0,other_countshould start from 1.count >= str.length - 1should becount < str.length - 1(this condition is completely unnecessary in your code becauseother_count < str.lengthshould be the terminating condition in the loop)and
other_count >= str.lengthshould beother_count < str.lengthHere's your corrected code
Here's an alternative approach
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.