Skip to content

AMS WEB FT FEB - Neil Gursahani #1503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Binary file added starter-code/.DS_Store
Binary file not shown.
Binary file added starter-code/jasmine/.DS_Store
Binary file not shown.
113 changes: 111 additions & 2 deletions starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,105 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;
} else if (num1 < num2) {
return num2;
} else {
return num1 && num2;
}
}


// Iteration #2: Find longest word
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
let emptyArray = [];
let singleWordArray = ['ironhack'];

function findLongestWord(array) {
if (array.length === 0) {
return null;
}


var longestWord = "";
for (var i = array.length-1; i >= 0; i--){
if (array[i].length >= longestWord.length) {
longestWord = array[i];
}
}
return longestWord;
}


console.log(findLongestWord(words));

// Iteration #3: Calculate the sum

const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
const numbers = [5];

// var sum = 0;
// numbers.forEach(array => console.log(array[i] + array[i+1]);

function sumNumbers(array) {
if (array.length === 0) {
return 0;
}

var sum = 0;
for (i=0; i<array.length; i++) {
sum += array[i];
}
return sum;
};

sumNumbers(numbers);

var sum = function sumNumbers(array) {
return array.reduce(function(a,b){
return a + b
}, 0);
if (array.length = 0){
return 0;
}
};


// Iteration #4: Calculate the average
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function countNumbers (numbersList) {
return numbersList.length;
}

function averageNumbers(arrayNumbers) {
console.log(sumNumbers()/countNumbers());
}

console.log(sumNumbers(numbersAvg));
console.log(countNumbers(numbersAvg));
console.log(averageNumbers(numbersAvg));




// Level 2: Array of strings
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];

function averageWordLength(array) {
let totalL = 0;
array.forEach(element => {
totalL += element.length
avgL = totalL / array.length
})
if (array.length === 0) {
return null;
}
return avgL
}
console.log(averageWordLength(wordsArr))


// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
Expand All @@ -26,8 +112,31 @@ const wordsUnique = [
'poison',
'communion',
'simple',
'bring'
'bring',
];
function uniquifyArray(orignalArr) {
let arr = [...orignalArr];
if (arr.length === 0) {
return null;
} else {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
let duplicate = arr.indexOf(arr[i], j);
if (duplicate > -1) {
arr.splice(duplicate, 1);
}
}
}
}
return arr;
}
uniquifyArray(wordsUnique);

// const uniqueSet = new Set (wordsUnique);

// const backToArray = [...uniqueSet];



// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
Expand Down
3 changes: 2 additions & 1 deletion starter-code/tests/functions-and-arrays.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ describe('Find the longest word', () => {
it('should return null when called with an empty array', () => {
expect(findLongestWord([])).toBe(null);
});

it('should return the word when called with a single-word array', () => {
expect(findLongestWord(['foo'])).toBe('foo');
});

//not sure if my answer for this task is correct
it('should return the first occurrence of the word when longest have multiple occurrences ', () => {
expect(findLongestWord(['foo', 'bar'])).toBe('foo');
expect(findLongestWord(['bar', 'foo'])).toBe('bar');
Expand Down