diff --git a/Problems/Seven-Boom/README.md b/Problems/Seven-Boom/README.md new file mode 100644 index 0000000..3cebe3e --- /dev/null +++ b/Problems/Seven-Boom/README.md @@ -0,0 +1,50 @@ +# Seven-Boom + +Create a function that takes an array of numbers and for every 7 found, add one "Boom!" to your result. If no 7 is found anywhere, return "there is no 7 in the array". + +## Infos which are maybe needed + +If e.g there is a `77` in the array, the count of 7's needs to increase by two. + +## Documentation + +### Solution Idea + +First, combine all numbers into a single string. +Then count how many times the digit `"7"` appears in that string. + +- If the count is `0`, return that there is no `"7"`. +- If the count is greater than `0`, return `"Boom!"` repeated as many times as the digit `"7"` appears. + +--- + +### [Implementation](./solver.ts) + +First, the array is joined into a single string using `array.join("")`. +Then we split the string again with `split("")`, which results in an array containing every single digit. + +Next, we use `reduce` to count how many times the digit `"7"` appears. +The reducer keeps track of the current count of sevens. + +If the current value is `"7"`, we increase the counter by 1. +Otherwise, we return the counter unchanged. + +Example reducer: + +.reduce((count, current) => (current === "7" ? count + 1 : count), 0) + +After this step, we have the total number of `"7"` digits stored in `sevenCount`. + +Finally, we check the value of this counter: + +- If it is `0`, we return the message that there is no `"7"`. +- If it is greater than `0`, we return `"Boom!"` repeated `sevenCount` times using: + +`"Boom! ".repeat(sevenCount).trim();` + +--- + +### Information's + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/how-does-google-maps-find-the-fastest-route) from February 25, 2026. diff --git a/Problems/Seven-Boom/seven-boom.test.ts b/Problems/Seven-Boom/seven-boom.test.ts new file mode 100644 index 0000000..3824e90 --- /dev/null +++ b/Problems/Seven-Boom/seven-boom.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "vitest"; +import sevenBoom from "./solver"; + +describe("Determines the correct count of sevens in the array", () => { + it("should return one 'Boom!' for the first array", () => { + expect(sevenBoom([1, 2, 3, 4, 5, 6, 7])).toEqual("Boom!"); + }); + + it("should return 'there is no 7 in the array' for the second array", () => { + expect(sevenBoom([8, 6, 33, 100])).toEqual("there is no 7 in the array"); + }); + + it("should return one 'Boom' for the third array", () => { + expect(sevenBoom([2, 55, 60, 97, 86])).toEqual("Boom!"); + }); + + it("should return three 'Boom' for the fourth array", () => { + expect(sevenBoom([7, 77, 100])).toEqual("Boom! Boom! Boom!"); + }); +}); diff --git a/Problems/Seven-Boom/solver.ts b/Problems/Seven-Boom/solver.ts new file mode 100644 index 0000000..cc591d5 --- /dev/null +++ b/Problems/Seven-Boom/solver.ts @@ -0,0 +1,9 @@ +export default function sevenBoom(array: number[]): string { + const sevenCount = array + .join("") + .split("") + .reduce((count, current) => (current === "7" ? count + 1 : count), 0); + + if (sevenCount === 0) return "there is no 7 in the array"; + else return "Boom! ".repeat(sevenCount).trim(); +} diff --git a/README.md b/README.md index 7d58c23..0901ee3 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha ## Problems -Solved: **25 problems** +Solved: **26 problems** -🟢 Easy: 19 +🟢 Easy: 20 🟠 Medium: 5 🔴 Hard: 1 @@ -41,6 +41,7 @@ Solved: **25 problems** | [Remove the last vowel](./Problems/Remove-The-Last-Vowel/README.md) | 🟢 Easy | | [Bingo Check](./Problems/Bingo-Check/README.md) | 🟢 Easy | | [Missing Number](./Problems/Missing-Number/README.md) | 🟢 Easy | +| [Seven Boom!](./Problems/Seven-Boom/README.md) | 🟢 Easy |