Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Total amount of points

/*
  Our football team finished the championship. 
  The result of each match look like "x:y". 
  Results of all matches are recorded in the collection.

  For example: ["3:1", "2:2", "0:1", ...]

  Write a function that takes such collection and counts the points of our team in 
  the championship. Rules for counting points for each match:

  if x > y: 3 points
  if x < y: 0 point
  if x = y: 1 point
*/

const points = games => {
  return games.map(game => {
    const [a, b] = game.split(':')
    return a > b ? 3 : a === b ? 1 : 0
  }).reduce((acc, cur) => acc + cur, 0)
}

 	// OR

const points = games => {
  let points = 0
  games.forEach(game => {
    const [a, b] = game.split(':')
    a > b ? points+=3 : a === b ? points+=1 : points+=0
  })
  return points
}

// With love @kouqhar
Comment

PREVIOUS NEXT
Code Example
Javascript :: 555 
Javascript :: nodejs stream pipeline with custom transform 
Javascript :: Count the number of child records on the each parent object 
Javascript :: javascript concat two htmlcollection 
Javascript :: prevent alpine js from rendering components during refresh 
Javascript :: iterate over array of html elements 
Javascript :: Return characters in a string in alphabetic order 
Javascript :: Use Prototype To Add A Property To Javascript Class 
Javascript :: how to run javascript in terminal 
Javascript :: what is so called abstractions in javascript 
Javascript :: iterating map javascript 
Javascript :: javascript math round 
Javascript :: js brightness 
Javascript :: add google map in react js 
Javascript :: for in loop 
Javascript :: javascript numbers 
Javascript :: values javascript 
Javascript :: vanilla js 
Javascript :: javascript output a message into console 
Javascript :: js date minus 18 years 
Javascript :: nodejs debug 
Javascript :: null is true or false javascript 
Javascript :: running webpack application on production server 
Javascript :: alertify js examples 
Javascript :: javascript foreach call specific value in array 
Javascript :: mongoose model schema 
Javascript :: react fontawesome exchange icon 
Javascript :: javascript first or default 
Javascript :: how to wait for the subscribe to finish before going back to caller method in angular 
Javascript :: js xor 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =