-
-
Notifications
You must be signed in to change notification settings - Fork 41
NW6 | Rabia Avci | JS2 Module | [TECH ED]Complete week 2 exercises | Week 2 #231
base: main
Are you sure you want to change the base?
Changes from all commits
7e76a14
7452103
03a926b
d0364d4
ec6268b
3b69df5
6cc14f8
03fc7df
73345de
4a0ac52
7b45aad
0497ad8
1ff1d45
634a6d3
e233f38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"liveServer.settings.port": 5501 | ||
} |
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,49 @@ | ||
function dedupe() {} | ||
//manual check | ||
// function dedupe(input) { | ||
|
||
// let output = []; | ||
|
||
// for(let i=0; i<input.length; i++){ | ||
// let duplicate = false; | ||
|
||
// for (let j = 0; j < output.length; j++) { | ||
// if (input[i] === output[j]) { | ||
// duplicate = true; | ||
// } | ||
// } | ||
|
||
// if (!duplicate) { | ||
// output.push(input[i]); | ||
// } | ||
// } | ||
|
||
// return output; | ||
// } | ||
|
||
|
||
function dedupe(input) { | ||
|
||
let output = []; | ||
|
||
for(let i=0; i<input.length; i++){ | ||
|
||
if (!output.includes(input[i])) { | ||
output.push(input[i]); | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,25 @@ E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8] | |
// Given an empty array | ||
// When passed to the dedupe function | ||
// Then it should return an empty array | ||
test.todo("given an empty array, it returns an empty array"); | ||
|
||
test("given an empty array, it returns an empty array", () => { | ||
expect(dedupe([])).toStrictEqual([]); | ||
}); | ||
|
||
// Given an array with no duplicates | ||
// When passed to the dedupe function | ||
// Then it should return a copy of the original array | ||
|
||
test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
expect(dedupe([5, 1, 2, 3, 8])).toStrictEqual([5, 1, 2, 3, 8]); | ||
}); | ||
|
||
|
||
// Given an array with strings or numbers | ||
// When passed to the dedupe function | ||
// Then it should remove the duplicate values | ||
|
||
|
||
test("given an array with strings or numbers, it removes the duplicate values", () => { | ||
expect(dedupe([5, 1, 'a', 'a', 2, 'b', 3, 3, 8, 8])).toStrictEqual([5, 1, 'a', 2,'b', 3, 8]); | ||
}); | ||
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Manual check | ||
// function max(input){ | ||
// if(input.length == 0){ | ||
// return -Infinity; | ||
// } | ||
|
||
// if(input.length == 1){ | ||
// return input[0]; | ||
// } | ||
// let maxNum = input[0]; | ||
// for (let i=0; i<input.length; i++){ | ||
// if(typeof input[i] !== 'string'){ | ||
// maxNum = input[i] | ||
// break; | ||
// } | ||
// } | ||
|
||
// for (let i=1; i<input.length; i++){ | ||
// if(input[i] > maxNum){ | ||
// maxNum = input[i]; | ||
// } | ||
// } | ||
// return maxNum; | ||
// } | ||
|
||
|
||
function max(input){ | ||
let output = []; | ||
for(let i=0; i<input.length; i++){ | ||
if (typeof input[i] !== 'string') { | ||
output.push(input[i]); | ||
} | ||
} | ||
return Math.max(...output); | ||
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. Amazing, don't forget to tidy commented out code :) |
||
} | ||
|
||
|
||
|
||
module.exports = max; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,45 @@ In this kata, you will need to implement a function that sums the numerical elem | |
E.g. max([30, 50, 10, 40]), target output: 50 | ||
E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) | ||
*/ | ||
const max = require("./max.js"); | ||
|
||
// Given an empty array | ||
// When passed to the max function | ||
// Then it should return -Infinity | ||
test.todo("given an empty array, returns -Infinity"); | ||
|
||
test("given an empty array, returns -Infinity", () => { | ||
expect(max([])).toBe(-Infinity); | ||
}); | ||
|
||
// Given an array with one number | ||
// When passed to the max function | ||
// Then it should return that number | ||
|
||
test("given an array with one number, returns number", () => { | ||
expect(max([2])).toBe(2); | ||
}); | ||
|
||
// Given an array with both positive and negative numbers | ||
// When passed to the max function | ||
// Then it should return the largest number overall | ||
|
||
test("given an array with both positive and negative number, returns max", () => { | ||
expect(max([-7, 6, 4, -3])).toBe(6); | ||
}); | ||
|
||
// Given an array with decimal numbers | ||
// When passed to the max function | ||
// Then it should return the largest decimal number | ||
|
||
test("given an array with decimal number, returns max", () => { | ||
expect(max([7.6, 1.6, 9.4, 3.4])).toBe(9.4); | ||
}); | ||
|
||
// Given an array with non-number values | ||
// When passed to the max function | ||
// Then it should return the max and ignore non-numeric values | ||
|
||
|
||
test("given an array with non-number values, returns max, ignore non-numeric values", () => { | ||
expect(max(["17", 12, "c", 5.4])).toBe(12); | ||
}); | ||
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
function sum(input) { | ||
let output = []; | ||
for (let i = 0; i < input.length; i++) { | ||
if (typeof input[i] !== "string") { | ||
output.push(input[i]); | ||
} | ||
} | ||
let total = 0; | ||
for (let i = 0; i < output.length; i++){ | ||
total = total + output[i]; | ||
} | ||
return total; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
module.exports = sum; | ||
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,25 +5,48 @@ In this kata, you will need to implement a function that sums the numerical elem | |
E.g. sum([10, 20, 30]), target output: 60 | ||
E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) | ||
*/ | ||
const sum = require("./sum.js"); | ||
|
||
// Acceptance Criteria: | ||
|
||
// Given an empty array | ||
// When passed to the sum function | ||
// Then it should return 0 | ||
|
||
test("given an empty array, returns 0 ", () => { | ||
expect(sum([])).toBe(0); | ||
}); | ||
|
||
|
||
// Given an array with just one number | ||
// When passed to the sum function | ||
// Then it should return that number | ||
|
||
test("given an array with just one number, returns that number ", () => { | ||
expect(sum([12])).toBe(12); | ||
}); | ||
|
||
|
||
// Given an array containing negative numbers | ||
// When passed to the sum function | ||
// Then it should still return the correct total sum | ||
|
||
test("given an array with both positive and negative number, returns correct total sum ", () => { | ||
expect(sum([-7, 6, 4, -3])).toBe(0); | ||
}); | ||
|
||
// Given an array with decimal/float numbers | ||
// When passed to the sum function | ||
// Then it should return the correct total sum | ||
|
||
test("given an array with decimal number, returns the correct total sum", () => { | ||
expect(sum([7.6, 1.6, 9.4, 3.4])).toBe(22); | ||
}); | ||
|
||
// Given an array containing non-number values | ||
// When passed to the sum function | ||
// Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
|
||
test("given an array with non-number values, returns the sum of the numerical elements, ignore non-numeric values", () => { | ||
expect(sum(["17", 12, "c", 5.4])).toBe(17.4); | ||
}); | ||
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
// 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 (const element of list){ | ||
if (element === target) { | ||
return index; | ||
return list.indexOf(element); | ||
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. Solid refactoring 👍 |
||
} | ||
} | ||
return -1; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// Predict and explain first... | ||
|
||
// Address is an object, and properties are accessed using their names, not numerical indices. | ||
|
||
// This code should log out the houseNumber from the address object | ||
// but it isn't working... | ||
// Fix anything that isn't working | ||
|
@@ -12,4 +14,6 @@ const address = { | |
postcode: "XYZ 123", | ||
}; | ||
|
||
console.log(`My house number is ${address[0]}`); | ||
console.log(`My house number is ${address.houseNumber}`); | ||
console.log(`My house number is ${address.city}`); | ||
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. 👍 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
function contains() {} | ||
function contains(obj, key) { | ||
if (typeof obj === "object" && Object.hasOwnProperty.call(obj, key)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
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. 👍 |
||
|
||
module.exports = contains; |
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.
Looking great 👍