-
-
Notifications
You must be signed in to change notification settings - Fork 41
NW6 | Fikret Ellek | JS-2-Module | Week-1 #183
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
function dedupe() {} | ||
function dedupe(list) { | ||
if (list.length === 0) { | ||
return list; | ||
} else { | ||
let dedupedList = []; | ||
for (let x of list) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please rename the |
||
if (!dedupedList.includes(x)) { | ||
dedupedList.push(x); | ||
} | ||
} | ||
return dedupedList; | ||
} | ||
} | ||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function maxNum(list) { | ||
let maximum = -Infinity; | ||
for (let x of list) { | ||
if (x > maximum && !x.isNaN) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, x is not really useful you should make all your functions and variables more meaningful. |
||
maximum = x; | ||
} | ||
} | ||
return maximum; | ||
} | ||
|
||
module.exports = maxNum; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function sum(list) { | ||
if (list.length === 0) { | ||
return 0; | ||
} else { | ||
let sumOfNum = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is good practice to declare your variables at the top at the beginning of the function. SO can you remove the sumOfNum var to line 2? |
||
for (let x of list) { | ||
if (!isNaN(x)) { | ||
sumOfNum += x; | ||
} | ||
} | ||
return sumOfNum; | ||
} | ||
} | ||
|
||
module.exports = sum; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const fs = require("fs"); | ||
const txtDoc = fs.readFileSync( | ||
"/home/eytha/cyf/Module-JS2/week-1/stretch/aoc-2018-day1/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)); | ||
console.log("Current working directory:", process.cwd()); | ||
|
||
module.exports = calculateFrequency; |
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); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!