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
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 becount < str.length - 1
(this condition is completely unnecessary in your code becauseother_count < str.length
should be the terminating condition in the loop)and
other_count >= str.length
should beother_count < str.length
Here'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.