2.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a1d9f98e819ed70a0e994e2 | Challenge 336: Horoscope Match | 28 | challenge-336 |
--description--
Given two star sign strings, return their compatibility percentage.
The signs are arranged in a wheel of 12 positions in this order: "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces", wrapping back to "Aries" after "Pisces". Find the shortest distance between the two signs and return the compatibility:
| Distance | Compatibility |
|---|---|
| 0 | "100%" |
| 1 | "40%" |
| 2 | "80%" |
| 3 | "30%" |
| 4 | "90%" |
| 5 | "20%" |
| 6 | "50%" |
--hints--
horoscopeMatch("Libra", "Sagittarius") should return "80%".
assert.equal(horoscopeMatch("Libra", "Sagittarius"), "80%");
horoscopeMatch("Gemini", "Scorpio") should return "20%".
assert.equal(horoscopeMatch("Gemini", "Scorpio"), "20%");
horoscopeMatch("Pisces", "Aries") should return "40%".
assert.equal(horoscopeMatch("Pisces", "Aries"), "40%");
horoscopeMatch("Capricorn", "Cancer") should return "50%".
assert.equal(horoscopeMatch("Capricorn", "Cancer"), "50%");
horoscopeMatch("Aquarius", "Aquarius") should return "100%".
assert.equal(horoscopeMatch("Aquarius", "Aquarius"), "100%");
horoscopeMatch("Virgo", "Taurus") should return "90%".
assert.equal(horoscopeMatch("Virgo", "Taurus"), "90%");
horoscopeMatch("Leo", "Scorpio") should return "30%".
assert.equal(horoscopeMatch("Leo", "Scorpio"), "30%");
--seed--
--seed-contents--
function horoscopeMatch(sign1, sign2) {
return sign1;
}
--solutions--
function horoscopeMatch(sign1, sign2) {
const signs = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"];
const a = signs.indexOf(sign1) + 1;
const b = signs.indexOf(sign2) + 1;
const diff = Math.abs(a - b);
const distance = Math.min(diff, 12 - diff);
const compatibility = { 0: "100%", 1: "40%", 2: "80%", 3: "30%", 4: "90%", 5: "20%", 6: "50%" };
return compatibility[distance];
}