Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 144 additions & 7 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,106 @@
// Progression #1: Greatest of the two numbers

function greatestOfTwoNumbers(num1,num2){
if(num1>num2)
{return num1;}
else
{return num2;}
}
// Progression #2: The lengthy word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];

function findScaryWord(words){
if(words.length==0){
return null;
}
for(i=0;i<words.length;i++){
if(words[0].length<words[i].length){
words[0] = words[i];
}
}
return words[0];
}
// Progression #3: Net Price
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function netPrice(numbers){
let sum=0;
for(i=0;i<numbers.length;i++){
if(numbers[i]!=0){
sum = sum +numbers[i];
}
}
return sum;
}
// Progression #4: Calculate the average
function add(numbers){
let sum=0;
for(i=0;i<numbers.length;i++){
if(typeof numbers[i]=='string'){
sum = sum + numbers[i].length;
}
else if(numbers[i]==true){
sum = sum+1;
}
else if(numbers[i]==false){
sum = sum +0;
}
else if(typeof numbers[i]==="number"){
sum = sum + numbers[i];
}
else throw Error("Unsupported data type sir or ma'am");
}
return sum;
}
// Progression 4.1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function midPointOfLevels(numbersAvg){
let sum = 0;
if(numbersAvg.length==0){
return null;
}
else{
for(let i=0;i<numbersAvg.length;i++){
sum = sum+numbersAvg[i];
}
return sum/numbersAvg.length;
}
}
// Progression 4.2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength(wordsArr){
let sum=0;
if(wordsArr.length==0){
return null;
}
else{
for(let i=0;i<wordsArr.length;i++){
sum = sum + wordsArr[i].length;
}
return sum/wordsArr.length;
}
}
function avg(mixedArr){
let sum=0;
if(mixedArr.length==0){
return null;
}
else{
for(let i=0;i<mixedArr.length;i++){
if(typeof(mixedArr[i]) == 'string'){
sum = sum+mixedArr[i].length;
}
else if(mixedArr[i]==true){
sum = sum + 1;
}
else if(mixedArr[i]==false){
sum = sum + 0;
}
else {
sum = sum+mixedArr[i];
}
}
var ans = parseFloat( (sum/mixedArr.length).toFixed(2));
return ans;
}
}
// Progression #5: Unique arrays
const wordsUnique = [
'bread',
Expand All @@ -28,10 +116,36 @@ const wordsUnique = [
'egg',
'flour'
];

function uniqueArray(wordsUnique){
let arr=[];
if(wordsUnique.length==0){
return null;
}
else{
for(let i=0;i<wordsUnique.length;i++){
for(let j=i+1;j<wordsUnique.length-1;j++){
if(wordsUnique[i] != wordsUnique[i+1]){
arr[i] = wordsUnique[i];
}
}
}
return arr;
}
}
// Progression #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];

function searchElement(wordsFind,_find){
if(wordsFind.length==0){
return null;
}
else{
for(let i=0;i<wordsFind.length;i++){
if(wordsFind[i]==_find){
return true;
}
}
}
}
// Progression #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -46,6 +160,15 @@ const wordsCount = [
'disobedience',
'matter'
];
function howManyTimesElementRepeated(wordsCount,_count){
let count=0;
for(let i=0;i<wordsCount.length;i++){
if(wordsCount[i]==_count){
count=count+1;
}
}
return count;
}

// Progression #8: Bonus

Expand All @@ -61,3 +184,17 @@ const matrix = [
[24, 55, 58, 05, 66, 73, 99, 26, 97, 17],
[21, 36, 23, 09, 75, 00, 76, 44, 20, 45]
];

function maximumProduct(matrix){
let sum=0;
for(let i=0;i<matrix.length;i++){
for(let j=0;j<matrix[i].length;j++){
let mmatrix = matrix[i];
sum = sum+mmatrix[j];
}
}
let len = matrix.length*matrix.length;
if(sum==len){
return 1;
}
}