From c92669d319910baed3b74863802886875c3f714f Mon Sep 17 00:00:00 2001 From: actuallyroy Date: Thu, 7 Jul 2022 18:39:24 +0530 Subject: [PATCH 1/2] Amit Kumar | Amity | A91005219029 --- tests/functions-and-arrays.spec.js | 109 +++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/tests/functions-and-arrays.spec.js b/tests/functions-and-arrays.spec.js index 2368533..e76de4c 100644 --- a/tests/functions-and-arrays.spec.js +++ b/tests/functions-and-arrays.spec.js @@ -12,6 +12,10 @@ function shuffle(currentArray) { return array; } +function greatestOfTwoNumbers(a, b){ + return Math.max(a, b); +} + describe('Find the greatest number', () => { it('should create a function named greatestOfTwoNumbers', () => { expect(typeof greatestOfTwoNumbers).toBe('function'); @@ -32,6 +36,20 @@ describe('Find the greatest number', () => { }); }); +function findScaryWord(arr){ + var flag = ""; + if(arr.length === 0){ + return null; + }else{ + for(var i = 0; i < arr.length; i++) { + if(arr[i].length > flag.length){ + flag = arr[i] + } + } + return flag; + } +} + describe('Find the scary word', () => { it('should create a function named findScaryWord', () => { expect(typeof findScaryWord).toBe('function'); @@ -59,6 +77,14 @@ describe('Find the scary word', () => { }); }); +function netPrice(price) { + sum = 0; + price.forEach(element => { + sum+=element; + }); + return sum; +} + describe('Calculate the sum of array of numbers', () => { it('should create a function named netPrice', () => { expect(typeof netPrice).toBe('function'); @@ -81,6 +107,27 @@ describe('Calculate the sum of array of numbers', () => { }); }); +function add(arr){ + sum = 0; + arr.forEach(element => { + switch (typeof element) { + case 'number': + sum += element; + break; + case 'string': + sum += element.length; + break; + case 'boolean': + sum+= element? 1 : 0; + break; + default: + throw new Error("Unsupported data type sir or ma'am"); + } + }); + return sum; + +} + describe('Bonus: Calculate the sum', () => { it('should create a function named add', () => { expect(typeof add).toBe('function'); @@ -124,6 +171,14 @@ describe('Bonus: Calculate the sum', () => { }); }); +function midPointOfLevels(arr){ + if(arr.length === 0){ + return null; + }else{ + return add(arr)/arr.length + } +} + describe('Calculate the average of an array of numbers', () => { it('should create a function named midPointOfLevels', () => { expect(typeof midPointOfLevels).toBe('function'); @@ -149,6 +204,10 @@ describe('Calculate the average of an array of numbers', () => { }); }); +function averageWordLength(arr){ + return midPointOfLevels(arr) +} + describe('Calculate the average of an array of strings', () => { it('should create a function named averageWordLength', () => { expect(typeof averageWordLength).toBe('function'); @@ -182,6 +241,11 @@ describe('Calculate the average of an array of strings', () => { }); }); +function avg(array) { + if(array.length === 0) return null; + return Number(midPointOfLevels(array).toFixed(2)) +} + describe('Bonus: Calculate the average of a mixed elements array', () => { it('should create a function named avg', () => { expect(typeof avg).toBe('function'); @@ -200,6 +264,18 @@ describe('Bonus: Calculate the average of a mixed elements array', () => { }); }); +function uniqueArray(array) { + if(array.length === 0) return null; + for(var i = 0; i < array.length; i++) { + for(var j = i + 1; j < array.length; j++) { + if(array[i] === array[j]) { + array.splice(j, 1) + } + } + } + return array; +} + describe('Unique array', () => { it('should create a function named uniquifyArray', () => { expect(typeof uniqueArray).toBe('function'); @@ -241,6 +317,18 @@ describe('Unique array', () => { }); }); +function searchElement(array, element){ + if(array.length === 0) return null; + result = false; + array.forEach(elem => { + if(elem == element){ + result = true; + } + }); + return result +} + + describe('Find elements', () => { it('should create a function named searchElement', () => { expect(typeof searchElement).toBe('function'); @@ -272,6 +360,16 @@ describe('Find elements', () => { }); }); +function howManyTimesElementRepeated(array, element) { + var count = 0; + array.forEach((elem) => { + if(element === elem){ + count++; + } + }) + return count; +} + describe('Count repetition', () => { it('should create a function named howManyTimesElementRepeated', () => { expect(typeof howManyTimesElementRepeated).toBe('function'); @@ -311,6 +409,17 @@ describe('Count repetition', () => { }); }); +function maximumProduct(arr) { + for(var i = 0; i < arr.length; i++) { + for(var j = 0; j < arr[i].length; j++){ + if(arr[i][j] == 0){ + return 0; + } + } + } + return 1; +} + describe('Bonus Quest - greatestProduct', () => { it('should create a function named maximumProduct', () => { expect(typeof maximumProduct).toBe('function'); From 2d14016afa418270aeb3fb81267acecaf8b3c107 Mon Sep 17 00:00:00 2001 From: actuallyroy Date: Thu, 7 Jul 2022 21:09:48 +0530 Subject: [PATCH 2/2] Amit Kumar | Amity | A91005219029 --- tests/functions-and-arrays.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functions-and-arrays.spec.js b/tests/functions-and-arrays.spec.js index e76de4c..4ff6a79 100644 --- a/tests/functions-and-arrays.spec.js +++ b/tests/functions-and-arrays.spec.js @@ -412,8 +412,8 @@ describe('Count repetition', () => { function maximumProduct(arr) { for(var i = 0; i < arr.length; i++) { for(var j = 0; j < arr[i].length; j++){ - if(arr[i][j] == 0){ - return 0; + if(arr[i][j] != 1){ + return null;; } } }