From 4ea797688553d1f8b07e0fe09e76d313de7d9db4 Mon Sep 17 00:00:00 2001 From: Stian Rusvik Date: Mon, 23 Sep 2024 09:59:12 +0200 Subject: [PATCH] finished --- .github/workflows/run-tests.yml | 18 +++++++++--------- README.md | 9 +++++---- spec/fizzbuzz.spec.js | 20 ++++++++++++++++++-- spec/support/jasmine.json | 8 ++------ src/fizzbuzz.js | 15 +++++++++++---- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 83a301b..60d8c0c 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,16 +1,16 @@ name: Node.js CI on: pull_request: - branches: [ main ] + branches: [main] jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: Use Node.js 17.x - uses: actions/setup-node@v2 - with: - node-version: 17.x - cache: 'npm' - - run: npm ci - - run: npm test \ No newline at end of file + - uses: actions/checkout@v2 + - name: Use Node.js 17.x + uses: actions/setup-node@v2 + with: + node-version: 17.x + cache: 'npm' + - run: npm ci + - run: npm test diff --git a/README.md b/README.md index 93b7d3b..1d100f3 100644 --- a/README.md +++ b/README.md @@ -13,30 +13,31 @@ $ npm ci ## Requirements -Inside the `src/fizzbuzz.js` file, write a program that populates the `answer` array with numbers from 1 to 15, *except*: +Inside the `src/fizzbuzz.js` file, write a program that populates the `answer` array with numbers from 1 to 15, _except_: - When the number is a multiple of three (e.g. 3, 6, 9, etc.), the number should be replaced with the word `Fizz` - When the number is a multiple of five (e.g. 5, 10, etc.), the number should be replaced with the word `Buzz` -- When the number is a multiple of both three *and* five (e.g. 15), the number should be replaced with the word `FizzBuzz` +- When the number is a multiple of both three _and_ five (e.g. 15), the number should be replaced with the word `FizzBuzz` ## Testing - At extremely regular intervals, make predictions about what the code you're writing is actually doing and verify whether you're right or wrong using `console.log` - A simple test has been created inside -`spec/fizzbuzz.spec.js`. You can look at it, but do not edit this file. + `spec/fizzbuzz.spec.js`. You can look at it, but do not edit this file. - When you think your solution is correct, run the test by executing the following command in your terminal: ```sh npm test ``` + You'll see a lovely green dot if the test passes successfully: ![](./images/passing-test.png) If the test fails, you'll see a lot of red! Don't worry, we'll learn how to understand error messages like the one below later in the course: -![](./images/failing-test.png) \ No newline at end of file +![](./images/failing-test.png) diff --git a/spec/fizzbuzz.spec.js b/spec/fizzbuzz.spec.js index ef2ecc5..6874912 100644 --- a/spec/fizzbuzz.spec.js +++ b/spec/fizzbuzz.spec.js @@ -1,5 +1,21 @@ const answer = require('../src/fizzbuzz.js') -it("FizzBuzzes", () => { - expect(answer).toEqual([1,2,'Fizz',4,'Buzz','Fizz',7,8,'Fizz','Buzz',11,'Fizz',13,14,'FizzBuzz']) +it('FizzBuzzes', () => { + expect(answer).toEqual([ + 1, + 2, + 'Fizz', + 4, + 'Buzz', + 'Fizz', + 7, + 8, + 'Fizz', + 'Buzz', + 11, + 'Fizz', + 13, + 14, + 'FizzBuzz' + ]) }) diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json index c9a6a72..d0398c8 100644 --- a/spec/support/jasmine.json +++ b/spec/support/jasmine.json @@ -1,11 +1,7 @@ { "spec_dir": "spec", - "spec_files": [ - "**/*[sS]pec.?(m)js" - ], - "helpers": [ - "helpers/**/*.?(m)js" - ], + "spec_files": ["**/*[sS]pec.?(m)js"], + "helpers": ["helpers/**/*.?(m)js"], "stopSpecOnExpectationFailure": false, "random": false } diff --git a/src/fizzbuzz.js b/src/fizzbuzz.js index e3ec3a8..d6da85e 100644 --- a/src/fizzbuzz.js +++ b/src/fizzbuzz.js @@ -1,10 +1,17 @@ const answer = [] // Write your code below this line - - - - +for (let i = 1; i <= 15; i++) { + if (i % 3 === 0 && i % 5 === 0) { + answer.push('FizzBuzz') + } else if (i % 3 === 0) { + answer.push('Fizz') + } else if (i % 5 === 0) { + answer.push('Buzz') + } else { + answer.push(i) + } +} // Don't touch the code below this line, we'll cover it later module.exports = answer