Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
Solving 8 kyu Define a card suit Code Wars (in javascript)?

My function is listed below, but it doesn't seem to work. The deck is pre-loaded, and test uses 4 cards to go through the function. Am I missing something?

Kata Code Wars

https://www.codewars.com/kata/5a360620f28b82a711000047

function defineSuit(card) {

var x = card.split('');

if (x === '♣') {

return 'clubs';

} else if ( x === '♦') {

return 'diamonds';

} else if ( x === '♥') {

return 'hearts';

} else {

return 'spades';

}

};

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use substr(-1) instead split('')[1]. Both will work the same, but substr(-1) is a bit faster

function defineSuit(card) {

   var suit = card.substr(-1);
   switch(suit){
      case '♣': return 'clubs';
      case '♦': return 'diamonds';
      case '♥': return 'hearts';
      default: return 'spades';
   }

}

"switch" fits better for this porpuse than "if/else". But remeber to use "breaks" if you are not returning a value after each case.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!