-
Notifications
You must be signed in to change notification settings - Fork 19
Jawad A. #21
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?
Jawad A. #21
Changes from all commits
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 |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| //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, 2, 3, 20, 100], 9)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { test, expect } from "vitest"; | ||
| import { calculateAverage } from "./calculateAverage.js"; | ||
|
|
||
| test("returns the average of numbers in an array", () => { | ||
| const numbers = [2, 4, 6]; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(4); | ||
| }); | ||
|
|
||
| test("returns the single element when array has one number", () => { | ||
| const numbers = [5]; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(5); | ||
| }); | ||
|
|
||
| test("returns null for empty array", () => { | ||
| const numbers = []; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(null); | ||
| }); | ||
|
|
||
| test("returns null for non-array input", () => { | ||
| const numbers = "123"; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(null); | ||
| }); | ||
|
|
||
| test("returns null when the array contains a non-number value", () => { | ||
| const numbers = [1, 2, "3"]; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(null); | ||
| }); | ||
|
|
||
| test("returns the average for array with negative numbers", () => { | ||
| const numbers = [-2, -4, -6]; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(-4); | ||
| }); | ||
|
|
||
| test("returns the average for array with floating point numbers", () => { | ||
| const numbers = [3.2, 1.2, 5.3]; | ||
|
|
||
| const result = calculateAverage(numbers); | ||
|
|
||
| expect(result).toBe(3.233333333333333); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| export function calculateAverage(numbers) { | ||
| if (!Array.isArray(numbers)) { | ||
| return null; | ||
| } | ||
| if (numbers.length === 0) { | ||
| return null; | ||
| } | ||
| let sum = 0; | ||
| for (const num of numbers) { | ||
| if (typeof num !== "number") { | ||
| return null; | ||
| } | ||
| sum += num; | ||
| } | ||
| return sum / numbers.length; | ||
| } | ||
| export function calculateAverage(numbers) { | ||
| if (!Array.isArray(numbers)) { | ||
| return null; | ||
| } | ||
|
|
||
| if (numbers.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| let sum = 0; | ||
|
|
||
| for (const num of numbers) { | ||
| if (typeof num !== "number") { | ||
| return null; | ||
| } | ||
| sum += num; | ||
| } | ||
|
|
||
| return sum / numbers.length; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,16 @@ | ||
| function countVowels(text) { | ||
| let count = 0; | ||
|
|
||
| for (let i = 0; i <= text.length; i++) { | ||
| const lowerCase = text.toLowerCase(); | ||
| for (let i = 0; i < lowerCase.length; i++) { | ||
| if ( | ||
| text[i] === "a" || | ||
| text[i] === "e" || | ||
| text[i] === "i" || | ||
| text[i] === "o" || | ||
| text[i] === "u" | ||
| lowerCase[i] === "a" || | ||
| lowerCase[i] === "e" || | ||
| lowerCase[i] === "i" || | ||
| lowerCase[i] === "o" || | ||
| lowerCase[i] === "u" | ||
| ) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| // you can use this function to generate a big array of the specified size | ||
| // It contains numbers from 0 to size - 1 | ||
| // Example: generateBigArray(5) will return [0, 1, 2, 3, 4] | ||
| function generateBigArray(size) { | ||
| const array = new Array(size); | ||
| for (let i = 0; i < size; i++) { | ||
| array[i] = i; | ||
| } | ||
| return array; | ||
| }; | ||
| } | ||
|
|
||
| export default generateBigArray; | ||
| export default generateBigArray; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,60 @@ | ||
| // Write your code here | ||
| // Use generateBigArray to create a big array numbers. | ||
| // Example: generateBigArray(1000000) will create an array of 1 million numbers. | ||
| import generateBigArray from "./bigArray.js"; | ||
|
|
||
| function getTargetByBinarySearch(array, target) { | ||
| let left = 0, | ||
| right = array.length - 1; | ||
| while (left <= right) { | ||
| const 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 false; | ||
| } | ||
|
|
||
| function getTargetByLinearSearch(array, target) { | ||
| for (let i = 0; i < array.length; i++) { | ||
| if (array[i] === target) { | ||
| return i; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| console.time("Linear Search 1k"); | ||
| getTargetByLinearSearch(generateBigArray(1000), 10540640); | ||
| console.timeEnd("Linear Search 1k"); | ||
|
|
||
| console.time("Linear Search 100k"); | ||
| getTargetByLinearSearch(generateBigArray(100000), 10540640); | ||
| console.timeEnd("Linear Search 100k"); | ||
|
|
||
| console.time("Linear Search 1M"); | ||
| getTargetByLinearSearch(generateBigArray(10000000), 10540640); | ||
| console.timeEnd("Linear Search 1M"); | ||
|
Comment on lines
+36
to
+38
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. Label says |
||
|
|
||
| console.time("Linear Search 10M"); | ||
| getTargetByLinearSearch(generateBigArray(10000000), 10540640); | ||
| console.timeEnd("Linear Search 10M"); | ||
|
|
||
| console.log("---"); | ||
|
|
||
| console.time("Binary Search 1k"); | ||
| getTargetByBinarySearch(getTargetByBinarySearch(1000), 10540640); | ||
|
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.
getTargetByBinarySearch(generateBigArray(1000), 10540640); |
||
| console.timeEnd("Binary Search 1k"); | ||
|
|
||
| console.time("Binary Search 100k"); | ||
| getTargetByBinarySearch(generateBigArray(100000), 10540640); | ||
| console.timeEnd("Binary Search 100k"); | ||
|
|
||
| console.time("Binary Search 1M"); | ||
| getTargetByBinarySearch(generateBigArray(10000000), 10540640); | ||
| console.timeEnd("Binary Search 1M"); | ||
|
Comment on lines
+54
to
+56
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. Same here: Label says |
||
|
|
||
| console.time("Binary Search 10M"); | ||
| getTargetByBinarySearch(generateBigArray(10000000), 10540640); | ||
| 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.
Array creation is included in the timing, this is wrong. Right now you are doing this:
This means you are measuring:
Array creation time+Search time. You should create the array first, then measure only the search: