Skip to content

Calculating with Functions #5

@rafaelkendrik

Description

@rafaelkendrik

This time we want to write calculations using functions and get the results. Let's have a look at some examples:

seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3

Requirements:

There must be a function for each number from 0 ("zero") to 9 ("nine")
There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby and Python)
Each calculation consist of exactly one operation and two numbers
The most outer function represents the left operand, the most inner function represents the right operand
Division should be integer division. For example, this should return 2, not 2.666666...:

eight(dividedBy(three()));

const num = n => cb => typeof cb === 'function' ? cb(n) : n

const zero = num(0)
const one = num(1)
const two = num(2)
const three = num(3)
const four = num(4)
const five = num(5)
const six = num(6)
const seven = num(7)
const eight = num(8)
const nine = num(9)

const plus = n2 => n1 => n1 + n2
const minus = n2 => n1 => n1 - n2
const times = n2 => n1 => n1 * n2
const dividedBy = n2 => n1 => Math.floor(n1 / n2)

added 2 tests to ensure that a number can be used both as second or first number.
and another one to ensure that divided number goes to floor.

https://www.codewars.com/kata/525f3eda17c7cd9f9e000b39/train/javascript

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions