Question

Here's my JavaScript question: What is the best way to write a comparator function in JavaScript...

Here's my JavaScript question:

What is the best way to write a comparator function in JavaScript that takes 2 strings s1 and s2 and returns true if s1 is alphabetically before s2, and false otherwise?

You cannot use the built in sort function, you must create your own sort function. Input strings s1 and s2 are not case sensitive and might not be equal length.

Here's two examples: compare('aaa', 'aab') returns true, and compare('aaa', 'aaab') returns true..

Homework Answers

Answer #1

<!DOCTYPE html>
<html>
<body>


<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function check(str1,str2) {

return str1.localeCompare(str2)==-1;
}
function myFunction(){
   alert(check("aaa","aab"));
}  
</script>

</body>
</html>

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
6.12 LAB: Grade distribution In this lab, you will write a JavaScript program that generates a...
6.12 LAB: Grade distribution In this lab, you will write a JavaScript program that generates a bar graph of letter grades from a distributions of scores. Implement the parseScores function (1 point) Implement the parseScores function to take a space-separated string of scores as an argument and return an array of score strings. Each score is a number in the range [0, 100]. Ex: "45 78 98 83 86 99 90 59" → ["45","78","98","83","86","99","59"] Hint: JavaScript's string split() function can...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
MATLAB Task 4: Plotting the trajectory of a ball thrown Write a user-defined function named throwBallFunc:...
MATLAB Task 4: Plotting the trajectory of a ball thrown Write a user-defined function named throwBallFunc: Which takes two arguments (parameters): Velocity and Angle Inside of the function, define some values (you can pick your own variable names) • Initial height of ball at release = 1.5m • Gravitational acceleration = 9.8m/s2 Next, create a time vector t that has 10000 values between 0 and 20, inclusive. The values must be sequential and not random If x is the vector...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
QUESTION 1 ? What is the relationship between family dysfunction and schizophrenia? a. ?Research has substantiated...
QUESTION 1 ? What is the relationship between family dysfunction and schizophrenia? a. ?Research has substantiated a link between family dysfunction and schizophrenia but can't say which causes the other. b. ?Family dysfunction is a major causative factor for schizophrenia. c. ?Research has failed to substantiate a direct causal link between family dysfunction and schizophrenia. d. ?Family dysfunction plays a minor role in developing schizophrenia. 1.00000 points    QUESTION 2 ? Chuck has no life plan; he simply lives from...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT