Exercises
Now that you've explored the fundamentals of time and space complexity with Big O notation, it's time to test your understanding! Analyze the following JavaScript functions and determine their time and space complexities. Experiment with different inputs to see how the functions behave.
1: determines if the array's size is odd or even:
const oddOrEven = (array) => array.length % 2 === 0 ? 'even' : 'odd';
2: calculates and returns the average of an array of numbers:
function calculateAverage(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum / array.length;
}
3: checks if two arrays have any common values:
function hasCommonElements(array1, array2) {
for (let i = 0; i < array1.length; i++) {
for (let j = 0; j < array2.length; j++) {
if (array1[i] === array2[j]) {
return true;
}
}
}
return false;
}
4: filters odd numbers from an input array...