Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38098
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2020-01-19 13:03 -0800 |
| References | <83a2fe4f-4564-4943-9515-b9bfc09b686a@googlegroups.com> |
| Message-ID | <8010fbd1-2ea8-4e26-8686-3316a1efa14f@googlegroups.com> (permalink) |
| Subject | Re: Compare the Triplets | Printing two values. |
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
On Sunday, January 19, 2020 at 2:05:15 AM UTC-6, manish pamnani wrote:
> Hey,
>
> I am solving a problem on hackerrank(https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen). The problem states to complete the function
> compareTriplets which must return an array of two integers, the first being ALice's score and the secoond being Bob's.
>
> compareTriplets has the following parameter(s):
>
> a: an array of integers representing Alice's challenge rating
> b: an array of integers representing Bob's challenge rating
>
> For more detail on the problem, refer to the following link:
> https://www.hackerrank.com/challenges/compare-the-triplets/problem?h_r=next-challenge&h_v=zen
>
> For the same challenge i wrote the following solution:
>
> ```
> function compareTriplets(a, b) {
> for (let i = 0; i < 4; i ++) {
> points=[];
> pointsArray=[];
> if (a[i] > b[i]) {
> points[i]=1;
> pointsArray.push(points[i])
> }
> if (a[i] === b[i]) {
> points[i]=0;
> }
> if (b[i] > a[i]) {
> points[i]=1;
> pointsArray.push(points[i])
> }
> return pointsArray;
> }
> }
> ```
>
> The problem states that the output should be like [1,1] i.e. [points earned by alice, points earned by bob]
>
> But somehow it displays a array with four elements with the values [1], [], [1], []
> Refer to https://ibb.co/GCkwM9j for further details.
>
> Can anybody suggest anything?
```
function compareTriplets(a, b) {
return a.map((value, i) =>
value < b[i] ? [0,1] : value == b[i] ? [0,0] :[1,0]
).reduce((sum, next) =>
[sum[0] + next[0], sum[1] + next[1]]
)
}
```
Map and reduce methods could be combined if you only want a single pass
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 00:05 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 10:08 +0100
Re: Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 09:08 -0800
Re: Compare the Triplets | Printing two values. manish pamnani <manishpamnani169@gmail.com> - 2020-01-19 09:09 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 19:10 +0100
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 09:42 -0800
Re: Compare the Triplets | Printing two values. "R.Wieser" <address@not.available> - 2020-01-19 10:47 +0100
Re: Compare the Triplets | Printing two values. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-19 13:03 -0800
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 14:05 -0800
Re: Compare the Triplets | Printing two values. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-01-19 15:21 -0800
Re: Compare the Triplets | Printing two values. Julio Di Egidio <julio@diegidio.name> - 2020-01-19 15:39 -0800
csiph-web