You are given an array of numbers and a threshold value.
Write a function that returns how many numbers in the array are greater than the threshold.
function countAboveThreshold(numbers, threshold) {
// Your code here
}
0countAboveThreshold([1,5,10,3],4)// returns 2
countAboveThreshold([7,8,9],10)// returns 0
countAboveThreshold([],5)// returns 0
Your task is to write unit tests for the function below using Vitest.
You must not change the function implementation.
Your job is to prove that it works correctly by writing meaningful test cases.
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;
}
null if:
Your unit tests should:
Make sure your tests cover at least the following scenarios:
[2, 4, 6] → 4[5] → 5null"123" or null → null[1, 2, "3"] → nullBy the end of this task, your test file should clearly demonstrate that:
In this task, you will practice finding and fixing errors by debugging. The code below runs, but it does not behave as expected.
The function below is supposed to return the number of vowels in a string.
function countVowels(text) {
let count = 0;
for (let i = 0; i <= text.length; i++) {
if (
text[i] === "a" ||
text[i] === "e" ||
text[i] === "i" ||
text[i] === "o" ||
text[i] === "u"
) {
count++;
}
}
return count;
}
itext[i]count increasescountVowels("hello")// returns 2
countVowels("javascript")// returns 3
countVowels("")// returns 0
text[i] have when i === text.length?