Forked from egor-miasnikov/gist:39255dee37aec1d5c973d87ff3e21cd0
Last active
February 3, 2019 10:21
-
-
Save vvscode/b9c9a1fef4fe92420584995b1b9f49c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
getChange | |
} = require('./getChange'); | |
const assert = require('assert'); | |
const TESTS = [{ | |
dealDetails: [1, 1], | |
change: [0, 0, 0, 0, 0, 0] | |
}, | |
{ | |
dealDetails: [1, 5], | |
change: [0, 0, 0, 0, 0, 4] | |
}, | |
{ | |
dealDetails: [3.67, 10], | |
change: [3, 0, 1, 1, 0, 6] | |
}, | |
]; | |
describe('getChange', () => { | |
it('getChange is a function', () => assert.equal(typeof getChange, 'function')); | |
TESTS | |
.forEach(({ | |
dealDetails, | |
change | |
}) => | |
it( | |
`should return correct change for [${dealDetails.join(', ')}]`, | |
() => assert.deepEqual(getChange(...dealDetails), change, `[${getChange(...dealDetails).join(', ')}] should be [${change.join(', ')}]`))); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SORTED_NOMINALS = [100, 50, 20, 10, 5, 1]; | |
const getCoinsNumberForSum = (sum, nominal) => Math.floor(sum / nominal); | |
const getChange = (price, income) => { | |
let changeSum = (income - price) * 100; | |
return SORTED_NOMINALS.map((nominal) => { | |
let nominalCount = getCoinsNumberForSum(changeSum, nominal); | |
changeSum = changeSum - nominalCount * nominal; | |
return nominalCount; | |
}).reverse(); | |
}; | |
module.exports = { | |
getChange, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####Описание: | |
Создайте функцию getChange, которая принимает 2 аргумента: первый стоимость товара и второй - оплата. | |
Функиция рассчитывает количество сдачи, которое вы получите исходя из входных данных. | |
Автомат может возвращать 1 коп, 5 коп, 10 коп, 20 коп, 50 коп, 1 руб. | |
Крупные деньги должны быть первыми в вашем ответе. | |
####Пример: | |
getChange(1, 1) //=> возвращает [0, 0, 0, 0, 0, 0] | |
getChange(1, 5) //=> возвращает [0, 0, 0, 0, 0, 4] | |
getChange(3.67, 10) //=> возвращает [3, 0, 1, 1, 0, 6] | |
Удачи |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment