export interface Box { code: string bomb?: boolean boxes?: Box[] } /** * Creates a box with optional boxes inside */ export const box = (code: string, boxes?: Box[]): Box => ({ code, ...( boxes ? { boxes } : {} )}); /** * Returns a copy of a box with bomb */ export function withBomb(box: Box) { return { ...box, bomb: true } } /** * Finds the bomb in a box */ export function pathToBomb(box: Box, path: string[] = []): string[] { const newPath = [...path, box.code] if (box.bomb) return newPath if (!Array.isArray(box.boxes)) return [] return traverseBoxes(box.boxes, newPath) } /** * Finds the bomb in an array of boxes */ export function traverseBoxes(boxes: Box[], path: string[] = []): string[] { return boxes.reduce((result: string[], box) => ( result.length > 0 ? result : pathToBomb(box, path) ), []) } /** * The main function */ export function findTheBomb(boxes: Box[]): string { if (boxes.length === 0) throw new Error('Empty array') return traverseBoxes(boxes).join(' > ') }- export interface Box {
- code: string
- bomb?: boolean
- boxes?: Box[]
- }
- /**
- * Creates a box with optional boxes inside
- */
export function box(code: string, boxes?: Box[]): Box {return boxes ? { code, boxes } : { code }}- export const box = (code: string, boxes?: Box[]): Box => ({ code, ...( boxes ? { boxes } : {} )});
- /**
- * Returns a copy of a box with bomb
- */
- export function withBomb(box: Box) {
- return { ...box, bomb: true }
- }
- /**
- * Finds the bomb in a box
- */
- export function pathToBomb(box: Box, path: string[] = []): string[] {
- const newPath = [...path, box.code]
- if (box.bomb) return newPath
- if (!Array.isArray(box.boxes)) return []
- return traverseBoxes(box.boxes, newPath)
- }
- /**
- * Finds the bomb in an array of boxes
- */
- export function traverseBoxes(boxes: Box[], path: string[] = []): string[] {
- return boxes.reduce((result: string[], box) => (
- result.length > 0 ? result : pathToBomb(box, path)
- ), [])
- }
- /**
- * The main function
- */
- export function findTheBomb(boxes: Box[]): string {
- if (boxes.length === 0) throw new Error('Empty array')
- return traverseBoxes(boxes).join(' > ')
- }
const calculateFine = (limit: number) => (speed: number) => { if (speed - limit >= 30) return 500; if (speed - limit >= 20) return 250; if (speed - limit >= 10) return 100; return 0; } export const checkIfAtOrBelowLimit = (acceleration: number[], limit: number): number[] => { return acceleration.map(calculateFine(limit)); }export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));- const calculateFine = (limit: number) => (speed: number) => {
- if (speed - limit >= 30) return 500;
- if (speed - limit >= 20) return 250;
- if (speed - limit >= 10) return 100;
- return 0;
- }
const CalculateFine = (speed: number, limit: number) => {if(speed >= limit + 30) return 500if (speed >= limit + 20 && speed <= limit + 29) return 250if (speed >= limit + 10 && speed <= limit + 19) return 100return 0- export const checkIfAtOrBelowLimit = (acceleration: number[], limit: number): number[] => {
- return acceleration.map(calculateFine(limit));
- }
Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)
Note: step != 0
Task
Create a function named
generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
function generateInterval(start, stop, step) { const arr = []; let value = start; for (let index = 0; index <= (stop - start) / step; index++) { arr.push(value); value += step; } return arr; }
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; const Test = require("@codewars/test-compat"); describe("generateInterval(2, 10, 2)", function() { it("startLessThenStop", function() { Test.assertSimilar(generateInterval(2, 10, 2), [2, 4, 6, 8, 10]); }); }); describe("generateInterval(2, 10, -2)", function() { it("startBiggerThenStop", function() { Test.assertSimilar(generateInterval(2, -10, -2), [2,0,-2,-4,-6,-8,-10]); }); });
Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)
Note: step != 0
Task
Create a function named
generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
function generateInterval(start, stop, step) {
const arr = [];
let value = start;
for (let index = 0; index <= (stop - start) / step; index++) {
arr.push(value);
value += step;
}
return arr;
}// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
const Test = require("@codewars/test-compat");
describe("generateInterval(2, 10, 2)", function() {
it("startLessThenStop", function() {
Test.assertSimilar(generateInterval(2, 10, 2), [2, 4, 6, 8, 10]);
});
});
describe("generateInterval(2, 10, -2)", function() {
it("startBiggerThenStop", function() {
Test.assertSimilar(generateInterval(2, -10, -2), [2,0,-2,-4,-6,-8,-10]);
});
});