1

i have this functions:

'use strict';
const toSuit1 = (card) => card.charAt(card.length - 1); //only color

function isFlush(hand) {
  const suits = hand.map(toSuit1).sort();
  let result;
  if (suits[0] === suits[4]) {
    result = 'true';
  } else {
    result = 'false';
  }
  return result;
}

console.log(isFlush(['7H', '2H', 'QH', '10H', '5H'])); // true
console.log(isFlush(['7H', '2H', 'QX', '10H', '5H'])); // false

Now I want to shorten this. But it doesn't work. But I still have problems with understanding and shortening. Can I make the const suit as an extra function?

'use strict';
const toSuit1 = (card) => card.charAt(card.length - 1); //only color
const suits = (hand) => hand.map(toSuit1).sort();
const isFlush = (suits) => suits[0] === suits[4] ? 'true' : 'false';

console.log(isFlush(['7H', '2H', 'QH', '10H', '5H'])); // true
console.log(isFlush(['7H', '2H', 'QX', '10H', '5H'])); // false
7
  • 2
    You never call the suits function. Furthermore, you shadow it with the parameter of isFlush, so you cannot call it. Commented Mar 24, 2021 at 10:24
  • Sometimes default sort doesn't work as expected.bugs.chromium.org/p/v8/issues/detail?id=90 You can use your own sort algoritm. sort((a,b)=> // compare ) Commented Mar 24, 2021 at 10:26
  • I think you do not need suits; you can check whether the hand is a flush by doing: const isFlush = (hand) => !hand.some(card => toSuit1(card) !== toSuit1(hand[0])). In other words you check whether there is at least one card whose color is different from the one of the first card Commented Mar 24, 2021 at 10:54
  • btw, why do you want a string as result? keeping it boolean would serve better later without convering a string back to boolean. Commented Mar 24, 2021 at 12:03
  • @Nina do you mean that? const isFlush = (suits) => { suits = sortSuits(suits); return suits[0] === suits[4]; } Commented Mar 24, 2021 at 13:54

1 Answer 1

2

It's fine to separate the sorting into its own function if you want. But you need to give the suits sorting function a different name, otherwise it isn't accessible from within isFlush, as there's already a local suits variable in there. And also you need to actually call the function in order to sort the data.

Try this:

'use strict';
const toSuit1 = (card) => card.charAt(card.length - 1); //only color
const sortSuits = (hand) => hand.map(toSuit1).sort();
const isFlush = (suits) => { 
  suits = sortSuits(suits); 
  return suits[0] === suits[4] ? 'true' : 'false'; 
}

console.log(isFlush(['7H', '2H', 'QH', '10H', '5H'])); // true
console.log(isFlush(['7H', '2H', 'QX', '10H', '5H'])); // false

However since your stated goal is "shortening" the code, it's debatable whether this is really the shortest version. If the sorting function doesn't need to be a separate function (in order to make it re-usable, for example), then you can save a line of code by re-integrating it back into the isFlush function, and just using the shortened ternary syntax instead of the if in the original:

'use strict';
const toSuit1 = (card) => card.charAt(card.length - 1); //only color
const isFlush = (suits) => { 
  suits = suits.map(toSuit1).sort(); 
  return suits[0] === suits[4] ? 'true' : 'false'; 
}

console.log(isFlush(['7H', '2H', 'QH', '10H', '5H'])); // true
console.log(isFlush(['7H', '2H', 'QX', '10H', '5H'])); // false

And we could take that one step further, if the toSuit1 function also isn't re-used, then again it can be simply integrated into the sorting process directly:

'use strict';
const isFlush = (suits) => { 
  suits = suits.map((card) => card.charAt(card.length - 1)).sort(); 
  return suits[0] === suits[4] ? 'true' : 'false'; 
}

console.log(isFlush(['7H', '2H', 'QH', '10H', '5H'])); // true
console.log(isFlush(['7H', '2H', 'QX', '10H', '5H'])); // false

As you can see from this example though, "shortening" the code can come at the cost of re-usability, and readability, and the ability to independently test different aspects of the functionality. I would question whether "shorter code" should really be a target in itself. You need to strike an appropriate balance between verbosity the other factors I mentioned, rather than having a simplistic plan to make everything shorter.

Sign up to request clarification or add additional context in comments.

2 Comments

Just for fun, I shortened it to this. Would definitely not recommend writing such code, though. Making the code readable and reusable is much more important.
@VLAZ absolutely agreed. Cool version btw, but I certainly wouldn't want to be the one testing it :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.