Course: How To Make JS Your Own. Comparing Strings


#1

https://www.codecademy.com/en/courses/javascript-intermediate-en-2nQQb/1/4#

I know this is an older track but I'm really enjoying the challenge and I only have two more lessons to go.

I have no idea how to go about this lesson. The instructions are a bit vague. Please help!


#2

difficult challenge, so we have two strings, this and str:

String.prototype.compareTo = function(str, caseSensitive) {
    console.log(this,str)
};

which we need to compare, and if caseSensitive is set to true, the comparison should be case insensitive.


#3

Thank you! Your hints helped me out.

Here's the solution I came up with. Please let me know if there's a more eloquent way to write this code:

String.prototype.compareTo = function(str, caseSensitive) {
	if(caseSensitive === true) {
	    return ((this == str) ? 0 : ((this > str) ? 1 : -1));
	} else {
	    return ((this.toUpperCase() == str.toUpperCase()) ? 0 : ((this.toUpperCase() > str.toUpperCase()) ? 1 : -1));
	}
};

#4

wow, nice code. I think it is really nice code

Can i recommend codewars to you? it has more nice challenges, i think you will enjoy it


#5

I have been meaning to check out Codewars but I wasn't sure if my knowledge was up to par yet. I'll give it a shot!


#6

you can give it a try, and if you like js challenge i can also highly recommend freecodecamp, just skip the html and stuff, just do the javascript, also a great challenge and learning source.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.