Skip to content

Instantly share code, notes, and snippets.

How many pages have I torn out?

Last month I borrowed a plenty of books from the library. They all were good books, packed with emotions and plot-twists. Unfortunately, at some points I got very angry/sad/disappointed, so I tore some pages out.

Now the library wants to know how many pages I have torn out for each book.

Your goal is to write a program, which takes a sorted, comma-delimited list of numbers as input and prints the minimum and maximum possible page count I could have torn out. Each line represents a book, each number represents a missing page from the book.

Example input:

'use strict'
// Check if every one of the countries have launched 20 or more rockets
// using a predicate function
const rockets = [
{ country: 'Russia', launches: 32 },
{ country: 'US', launches: 23 },
{ country: 'China', launches: 16 },
{ country: 'Europe(ESA)', launches: 7 },
{ country: 'India', launches: 4 },
'use strict'
// Check if some of the countries have launched 20 or more rockets
// using a predicate function called isAggressive
const rockets = [
{ country: 'Russia', launches: 32 },
{ country: 'US', launches: 23 },
{ country: 'China', launches: 16 },
{ country: 'Europe(ESA)', launches: 7 },
{ country: 'India', launches: 4 },
'use strict'
// Starting array
const ages = [7, 24, 21, 18, 22]
// Use find to return the age of first age over 21
// using a predicate function
const isAdult = (age) => {
return age >= 21
}
'use strict'
// Starting array
const ages = [7, 24, 21, 18, 22]
// Use indexOf to find the position of 21
ages.indexOf(21) // 2
// Use findIndex to find the position of first age over 21
// using a predicate function
'use strict'
// Example array
const numsArray = [0, 1, 2, 3, 4]
// Filter for odd elements in an array using predicate function
const isOdd = (num) => {
return num % 2
}
odds = numsArray.filter(isOdd)
'use strict'
// Example array
const numsArray = [0, 1, 2, 3, 4]
// Find the sum of all elements in an array using anonymous arrow function
const sum = numsArray.reduce((prev, curr) => prev + curr)
// Find the sum of all elements in an array using named arrow function
const add = (prev, curr) => {
return prev + curr
  • Simplicity !== Easy
  • Easy is short term and can sometimes even introduce complexity
  • Simplicity allows for easy while avoiding complexity
  • Complect == tangling/braiding together
  • Complexity is the result of something that has been complected
  • Untangle complected code (e.g. decoupling of components)
  • Do not chain calls (i.e. Law of Demeter)
  • Consider Channels as form of asynchronous flow control
  • Ask yourself all the time: "can this thing be moved? does it have well defined boundaries?"