How can I improve solution for exercise?

Hi! Everyone. Who can tell me, how can I improve the solution for this exercise “Basic Algorithm Scripting: Truncate a String”?

here is an existing solution:

function truncateString(str, num) {
  if (str.length <= num) {
    return str;
  } else {
    return str.slice(0, num > 3 ? num - 3 : num) + '...';

and my solution:

function truncateString(str, num) {
return str.length > num ? str.slice(0, num) + ‘…’ : str;

Is my solution better than existing isn’t it?

don’t use ternary operators to do anything but return true or false

The solution that is not yours seems wrong, I think it is from the previous version of the challenge

Yours works, there is nothing wrong with it

Can I ask you why?
I am learning and I have never heard of it

it just makes it much easier to read and less buggy. i learned this after being turned down in an interview where one of the things i did wrong was use ? : to set a variable

they broke my code down and tore me a new one. still too afraid to finish reading their assessment.

1 Like

Using ternary operator to return a value is perfectly fine.

Doing variable assignment inside ternary is considered bad.

// bad :(
let foo = 5;
str.length > num ? foo = 6 : foo = 0;
1 Like

yeh thats what I did.
dont do that

I imagine I can instead do like this?
foo = str.length > num ? 6 : 0

1 Like

Yes, that’s correct.

If you use linter (and if you don’t, you should start using it) you’ll get an error if you’ll attempt to do assignment inside ternary.

Thanks everyone for your reply! …but, I don’t understood. Can I suggest my code as advanced answer?

ps. English isn’t my native lang. If I made mistake sorry.

Ok, You mean that my code is unreadable? Professional developers used ternary operator for this kind of code: “condition ? true : false” ?

Its not unreadable but the less time i have to spend trying to figure what what the line will do the better

I want to say, keep it at one idea at a time, if that makes sense

I want to read from the top down as easily as possible. Think about when you have a file with maybe hundreds or even thousands of lines. You want to read it as easily as you read english

Do you think there are moments where it’s completely fine?

Ever since I started with JS I’ve been slightly obsessed with one-line solutions.

Also, would love to hear more about your interview experience.

one line solutions are awesome if you’re the only one that has to look at it but chances are that in a professional environment lots of other people will have to look at your code. you also want people to be able to help you fix your bugs and vice versa.

at the end of the day coding is a team sport. that’s why languages are standardized in the first place. so that many people can look at the same code and know exactly what it does. so you need to always think about things you can do to make your code easy to read for the people who will come after you.

lots of companies have their own rules about how they want code to be written. so its good to learn about the “good” and “bad” ways and try to follow best practices

my interview experience has been pretty rough so far. I need to start cranking out projects more often so that I can show people what I’ve been working on. I think people who hire don’t really care about your code. They wan’t to see something you build that they can use and see working and in motion.

When you start aiming for a complete project then the learning really takes care of itself. It’s kind of an annoying cliche when people say you should just start building stuff but it’s really true. Build until you get hired then build some more.

Can’t believe I forgot one of the most simple yet important things about coding.

And yes, it’s true. I’ve watched plenty of videos advising and been in plenty of interviews where the question of “So you have projects to showcase?” has been asked.

In the end, code is code. Eyes are set on the project. Nobody will be impressed for the list of technologies you know if you don’t have any projects to confirm your knowledge.

One of my plans is to turn my GitHub io page into a showcase page.

Whatever immediate gratification you get from writing cryptically fancy or clever code is nothing compared to the headache 6 months from now when you or somebody else have to read the code.

Simple, legible code is way more valuable then clever code which places an unnecessary cognitive load on the reader. I’m not saying you can’t write clever code, just don’t sacrifice readability for it. At least not if the code is written for more then just an endorphin rush. Just my two cents.

I wouldn’t say that ternary operator is cryptic, fancy, “clever”, advanced or whatever, it’s just a matter of exposure - just like getting used to arrow functions, map/filter/reduce etc. For example when writing React using ternary is very common pattern for conditional rendering.

Just don’t write nested ternaries :nauseated_face:

BTW, here’s a function that checks if a letter is uppercase and returns true or false:

const fn = ch => ch < {};

This is what I would call cryptic, fancy, “clever”, advanced.

I wasn’t talking about the ternary I just meant code in general. That is, writing code to satisfy some ego desirer as opposed to writing code that has value for everyone.

Ok, thanks. I understood you.