Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1,468 changes: 1,468 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "c55-core-week-5",
"version": "1.0.0",
"description": "HackYourFuture Core program week 5 assignment",
"main": "index.js",
"scripts": {
"test": "vitest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dianadenwik/c55-core-week-5.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs",
"bugs": {
"url": "https://github.com/dianadenwik/c55-core-week-5/issues"
},
"homepage": "https://github.com/dianadenwik/c55-core-week-5#readme",
"devDependencies": {
"vitest": "^4.0.18"
}
}
17 changes: 16 additions & 1 deletion task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
//Your code here
function countAboveThreshold(numbers, threshold) {
let count = 0;

for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > threshold) {
count++;
}
}

return count;
}

console.log(countAboveThreshold([1, 5, 10, 3], 4));
console.log(countAboveThreshold([7, 8, 9], 10));
console.log(countAboveThreshold([], 5));

46 changes: 46 additions & 0 deletions task-2/calculate.test.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor change: Naming convention for test files usually follows the file it is testing. In this case that would be calculateAverage.test.js.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { test, expect } from "vitest";
import { calculateAverage } from "./calculateAverage.js";

/*

- Returns the average of numbers in an array
- Returns `null` if:
- the input is not an array
- the array is empty
- the array contains a non-number value
*/

test("returns the average of numbers in an array", () => {
const numbers = [2, 4, 6];
const result = calculateAverage(numbers);

expect(result).toBe(4);
});

test("An array with a single number", () => {
const numbers = [2];
const result = calculateAverage(numbers);

expect(result).toBe(2);
});

test("Returns `null` if the input is not an array", () => {
const numbers = "2";
const result = calculateAverage(numbers);

expect(result).toBe(null);
});

test("Returns `null` if the array is empty", () => {
const numbers = [];
const result = calculateAverage(numbers);

expect(result).toBe(null);
});

test("Returns `null` if the array contains a non-number value", () => {
const numbers = ["apple"];
const result = calculateAverage(numbers);

expect(result).toBe(null);
});
1 change: 1 addition & 0 deletions task-2/calculateAverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export function calculateAverage(numbers) {

return sum / numbers.length;
}

22 changes: 16 additions & 6 deletions task-3/count-vowels-debug.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
function countVowels(text) {
let count = 0;

for (let i = 0; i <= text.length; i++) {
for (let i = 0; i < text.length; i++) { // <= = <
if (
text[i] === "a" ||
text[i] === "e" ||
text[i] === "i" ||
text[i] === "o" ||
text[i] === "u"
text[i].toLowerCase() === "a" ||
text[i].toLowerCase() === "e" ||
text[i].toLowerCase() === "i" ||
text[i].toLowerCase() === "o" ||
text[i].toLowerCase() === "u"
) {
count++;
}
}

return count;
}



console.log(countVowels("hello"));
console.log(countVowels("javascript"));
console.log(countVowels("JLOOIKANJDK"));
console.log(countVowels("EIOAEYoiibtd"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possible test case is an empty text.




4 changes: 4 additions & 0 deletions task-4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{
"type": "module"
}
100 changes: 99 additions & 1 deletion task-4/search-experiment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,101 @@
// Write your code here
// Use generateBigArray to create a big array numbers.
// Example: generateBigArray(1000000) will create an array of 1 million numbers.
// Example: generateBigArray(1000000) will create an array of 1 million numbers.

import generateBigArray from './bigArray.js';

const array1k = generateBigArray(1000);
const array100k = generateBigArray(100000);
const array1M = generateBigArray(1000000);
const array10M = generateBigArray(10000000);


/*linear search
function linear_search(array, target) is
for i = 0 to array.length - 1 do
if array[i] = target
return target
return unsuccessful
*/

function linear_search(array, target) {
for (let i = 0; i< array.length; i++){
if(array[i] === target) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pay attention to code formatting. Indented code is easier to read.

return i;
}
}
return 'unsuccessful';
}


/*Binary Search
function binary_search(array, target) is
left := 0
right := array.length - 1
while left ≤ right do
middle := left + floor((right - left) / 2)
if array[middle] < target then
left := middle + 1
else if array[middle] > target then
right := middle − 1
else:
return middle
return unsuccessful
*/

function binary_search(array, target) {
let left = 0;
let right = array.length - 1;

while (left <= right) {
let middle = left + Math.floor((right - left) / 2);

if (array[middle] < target) {
left = middle + 1;
} else if (array[middle] > target) {
right = middle - 1;
} else {
return middle;
}
}
return 'unsuccessful';
}




console.time('Linear Search 1k');
linear_search(array1k, 7824);
console.timeEnd('Linear Search 1k');

console.time('Linear Search 100k');
linear_search(array100k, 45734);
console.timeEnd('Linear Search 100k');

console.time('Linear Search 1M');
linear_search(array1M, 658373924);
console.timeEnd('Linear Search 1M');

console.time('Linear Search 100M');
linear_search(array10M, 745984);
console.timeEnd('Linear Search 100M');

console.log(`--------------------------`);


console.time('Binary Search 1k');
binary_search(array1k, 4638);
console.timeEnd('Binary Search 1k');

console.time('Binary Search 100k');
binary_search(array100k, 547445);
console.timeEnd('Binary Search 100k');

console.time('Binary Search 1M');
binary_search(array1M, 643647548);
console.timeEnd('Binary Search 1M');

console.time('Binary Search 10M');
binary_search(array10M, 6687998);
console.timeEnd('Binary Search 10M');