Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

NW6 | Fikret Ellek | JS-2-Module | Week-1 #183

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
week 1 done
  • Loading branch information
fikretTest committed Jan 8, 2024
commit 6f8a3ecc97aa926fa45bc79a57c672beb90e589f
6 changes: 3 additions & 3 deletions week-1/refactor/find.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Refactor the implementation of find to use a for...of loop

function find(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (let x of list) {
const element = x;
if (element === target) {
return index;
return list.indexOf(x);
}
}
return -1;
Expand Down
19 changes: 19 additions & 0 deletions week-1/stretch/aoc-2018-day1/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");
const txtDoc = fs.readFileSync("./input.txt", "utf8");
const txtList = txtDoc.split("\n");
const numList = txtList.map(Number);

function calculateFrequency(list) {
let sum = 0;

for (let x of list) {
if (!isNaN(x)) {
sum += x;
}
}
return sum;
}

console.log(calculateFrequency(numList));

module.exports = calculateFrequency;
34 changes: 34 additions & 0 deletions week-1/stretch/aoc-2018-day1/solution.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const calculateFrequency = require("./solution");

describe("testing the calculateFrequency function", () => {
test("testing with an empty array", () => {
const input = [];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with positive numbers", () => {
const input = [1, 2, 3, 4];
const expectedOutput = calculateFrequency(input);
const targetOutput = 10;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with negative numbers", () => {
const input = [-1, -2, -3, -4];
const expectedOutput = calculateFrequency(input);
const targetOutput = -10;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with positive and negative numbers", () => {
const input = [-1, 2, 3, -4];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
test("testing with an array with characters", () => {
const input = ["a", "b", "c", "d"];
const expectedOutput = calculateFrequency(input);
const targetOutput = 0;
expect(expectedOutput).toEqual(targetOutput);
});
});