It is possible to write more short function

It is possible to write more short function
0

#1

Continuing the discussion from freeCodeCamp Algorithm Challenge Guide: Confirm the Ending:

function confirmEnding(str, target) {
return str.slice(-target.length)===target;
}


#2

Aside from using the shorter built-in endsWith function, it’s probably as short as it can get (or make it tad shorter by using == instead of ===).


#3

I am sorry, but I did not understand You clearly.
Is my solution the shortest or not?

function confirmEnding(str, target) {
return str.slice(-target.length)===target;
}


#4

I’d say it depends on how you look at it. In terms of the number of steps the function does, it’s probably as short as it can get. But in terms of the number of characters used, it can be made shorter, but at the expense of readability of your code.


#5

You are right. It is possible to use using "== " instead of “===”.
Letter case checking still works:

function confirmEnding(str, target) {
return str.slice(-target.length)==target; //"==" is enough
}


#6


It is funny but copying code from my own message in the forum’s form to my Javascript IDE gave me an error of running code which was running without errors earlier.
I was stucked for a while but found the issue: quotes were transformed from one type to another without warning. Be careful!


#7

Yes, the forum transforms quotes. To post code, you can use the </> button on the forum’s editor.


#8

Thank You for the hint!


#9

You can also get rid of curly braces and return keyword btw:

const confirmEnding = (str, target) =>
  str.substr(-target.length) == target;

#10

Pretty cool and it works fine too!
But we have to define a function, not a constant (Restrictions on the input conditions).


#11

It’s also a way to declare functions. It’s what’s usually called an arrow function, and has some differences with the usual function declaration, but for the most part it works the same.

You can also define a function like this:

var confirmEnding = function(str, target) {...}

#12

Thank You, guys, for the explanation!


#13
confirmEnding=(str,target)=>str.substr(-target.length)==target;

I suppose that this is the shortest version which can pass the tests for this moment.
Guys, I offer to start a contest to make this code snippet shorter :grin:


#14
confirmEnding=(a,b)=>a.substr(-b.length)==b;

Now if only the function name can be changed :joy:

Actually we can go shorter!

confirmEnding=(a,b)=>a.slice(-b.length)==b

#15
confirmEnding=(a,b)=>a.substr(-b.length)==b;

This would also work, but you get to the point of being near unreadable :slight_smile:

Edit…Damnit Kev


#16

I already had this in mind when OP asked their question :stuck_out_tongue:


#17

depending on the next line, the semicolon is likely redundant.


#18

Guys, You are awesome! :star_struck:


#19

@lynxlynxlynx is correct.
Javascript compilers often put an inferred semi colon in there for you (you don’t see it in your code).

I have been reading the following book:

It covers some of the unusual (unique?) features of javascript, things that might not be intuitive and can lead to chasing ones tail (nuances?).

Cheers,
WWC


#20

Do You have a used book
"More buying choices for
Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript (Effective Software Development Series) (Paperback)
by David Herman" for sale?