Company assignment for TANDEMLOOP
The problem requires creating a small calculator that performs basic operations such as addition, subtraction, multiplication, and division. The calculator takes inputs a and b as numbers and the type of operation as a string.
- Create a class called
BasicCalculator. - Implement the constructor to initialize the
a,b, andoperationproperties of the calculator. - Implement the
calculate()method that performs the calculation based on the provided operation. - Use a switch statement to handle different operations and calculate the result accordingly.
- Return the result of the calculation.
Code
class BasicCalculator {
/**
* @param {number} a - First parameter for the operation
* @param {number} b - Second parameter for the operation
* @param {string} operation - Operation name
* */
constructor(a = 0, b = 0, operation) {
if (+a === NaN || +b === NaN) throw Error('Wrong parameters provided');
this.a = +a;
this.b = +b;
this.operation = operation;
}
/**
* Perform the calculation based on the provided operation
* @param {string} operation - Optional operation name, defaults to the initial operation provided in the constructor
* @returns {number} - Result of the calculation
*/
calculate(operation = this.operation) {
// Convert the operation name to lowercase
operation = operation.toLowerCase();
if (typeof operation !== 'string') throw Error('Wrong parameters provided');
let result = 0;
switch (operation) {
case 'addition':
result = this.a + this.b;
break;
case 'subtraction':
result = this.a - this.b;
break;
case 'multiplication':
result = this.a * this.b;
break;
case 'division':
result = this.a / this.b;
break;
default: {
throw Error('Please provide a proper "Operation" from the options: "addition", "subtraction", "multiplication", or "division"!');
}
}
// Capitalize the first letter of the operation name
const operationName = operation.charAt(0).toUpperCase() + operation.slice(1);
console.log(`${operationName} result: ${result}`);
return result;
}
}The problem requires generating a series of odd numbers based on the input value a.
- Implement the
getOddNums()function that takes an inputnas the number of odd numbers to generate. - Create an array to store the odd numbers.
- Use a loop to generate the odd numbers and store them in the array.
- Return the array of odd numbers.
Code
/**
* Generate `n` odd numbers.
* @param {number} n - Number of odd numbers to generate.
* @returns {number[]} - Array containing `n` odd numbers.
*/
function getOddNums(n = 1) {
// Create an array to store the odd numbers
let oddNumbers = new Array(n);
// Generate the odd numbers using a loop
for (let i = 0; i < n; i++) {
oddNumbers[i] = 2 * i + 1;
}
// Log the generated odd numbers to the console
console.log(`Output for input ${n}: ${oddNumbers.join(", ")}`);
// Return the array of odd numbers
return oddNumbers;
}This problem is similar to Problem 2 but has a slightly different pattern in the output. The series of odd numbers should be generated until a is reached, but for even values of a, the series should go up to a-1.
- Implement the
getOddNums()function similar to Problem 2. - Check if the input
nis odd or even. - If
nis odd, generatenodd numbers as in Problem 2. - If
nis even, generaten-1odd numbers to follow the desired pattern. - Return the array of odd numbers.
Code
/**
* Generate `n` odd numbers.
* @param {number} n - Number of odd numbers to generate.
* @returns {number[]} - Array containing `n` odd numbers.
*/
function getOddNums(n = 1) {
// Update the length depending on `n` is a odd or even
const length = (n % 2 ? n : n - 1);
// Create an array to store the odd numbers
let oddNumbers = new Array(length);
// Generate the odd numbers using a loop
for (let i = 0; i < length; i++) {
oddNumbers[i] = 2 * i + 1;
}
// Log the generated odd numbers to the console
console.log(`Output for input ${n}: ${oddNumbers.join(", ")}`);
// Return the array of odd numbers
return oddNumbers;
}The problem requires counting the total occurrences of numbers in the given array that are multiples of the numbers [1, 2, 3, 4, 5, 6, 7, 8, 9].
- Implement the
getCountOfMultiples()function that takes an input arrayinputArr. - Create an object called
recordMultiplesto store the count of multiples for each number. - Initialize the
recordMultiplesobject with 0 for numbers 1 to 9. - Iterate through the
inputArrand for each number, check its divisibility by numbers 1 to 9. - If a number is divisible by a particular divisor, increment the count in the
recordMultiplesobject for that divisor. - Log the
recordMultiplesobject to the console. - Return the
recordMultiplesobject.
Code
/**
* Get the total count of numbers in the input array that are multiples of [1, 2, 3, 4, 5, 6, 7, 8, 9].
* @param {Array} inputArr - Array of numbers.
* @returns {Object} - Object with the total counts of numbers that are multiples of [1, 2, 3, 4, 5, 6, 7, 8, 9].
*/
function getCountOfMultiples(inputArr = []) {
// Create an object to record the count of multiples for each number
const recordMultiples = {};
// Initialize the record object with 0 for all the numbers
for (let i = 1; i <= 9; i++) {
recordMultiples[i] = 0;
}
// Iterate through the input array
for (let num of inputArr) {
// Check divisibility of the number by each number from 1 to 9
for (let i = 1; i <= 9; i++) {
if (num % i === 0) {
// Increment the count if the number is divisible
recordMultiples[i]++;
}
}
}
// Log the output
console.log('Output:', recordMultiples);
// Return the record object
return recordMultiples;
}The time complexity and space complexity for each problem are as follows:
-
Problem 1:
- Time Complexity: O(1)
- Space Complexity: O(1)
-
Problem 2:
- Time Complexity: O(n)
- Space Complexity: O(n)
-
Problem 3:
- Time Complexity: O(n)
- Space Complexity: O(n)
-
Problem 4:
- Time Complexity: O(n)
- Space Complexity: O(1) (constant space for the
recordMultiplesobject)