-
Notifications
You must be signed in to change notification settings - Fork 19
Diana C. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Diana C. #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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" | ||
| } | ||
| } |
| 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)); | ||
|
|
| 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); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ export function calculateAverage(numbers) { | |
|
|
||
| return sum / numbers.length; | ||
| } | ||
|
|
||
| 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")); | ||
|
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. Another possible test case is an empty text. |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
|
|
||
| { | ||
| "type": "module" | ||
| } |
| 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) { | ||
|
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. 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'); | ||
|
|
||
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.
Minor change: Naming convention for test files usually follows the file it is testing. In this case that would be
calculateAverage.test.js.