Skip to content

AMS FEB FT FEB - Yvana Wagter #1507

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 6 commits into from
Closed
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
125 changes: 125 additions & 0 deletions starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,107 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers(num1, num2){
if (num1 > num2) {
return num1;
} else if (num2 > num1){
return num2;
} else
return num1;
return num2;
}

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

function findLongestWord (array){
if (array.length === 0){
return null;
} else if (array.length > 0) {
var longestWord = "";
array.forEach(function(word){
if (word.length > longestWord.length) {
longestWord = word;
return longestWord;
}
});
return longestWord;
}
}


// Iteration #3: Calculate the sum

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

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

function sum (array){
if (array.length === 0){
return 0;
} else if (array.length == 1) {
return array [i];
} else if (array.length > 1){
var totalSum = 0;
for (let i = 0; i < array.length; i++){
switch (typeof(array[i])){
case "boolean":
if (array[i]){
totalSum++;
}
case "string":
totalSum += array[i].length;
break;
case "number":
totalSum += array[i];
break;
default:
throw new Error();
}
}
}
return totalSum;
}

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

function averageNumbers(array){
if (array.length === 0){
return null;
} else {
average = sumNumbers(array) / array.length;
return average;
}
}

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

function averageWordLength (array){
if (array.length === 0){
return null;
} else {
average = array.join('').length / array.length
return average;
}
}

function avg (array){
if (array.length === 0){
return null;
} else {
average = (sum(array) / array.length);
return average;
}
}


// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
Expand All @@ -29,9 +117,33 @@ const wordsUnique = [
'bring'
];


function uniquifyArray (array){
if (array.length === 0){
return null;
} else {
return array.filter((a,b) => array.indexOf(a) === b)
}
}

console.log(uniquifyArray(words));

// Iteration #6: Find elements
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];


function doesWordExist (array, word){
if (array.length === 0){
return null;
} else {
if (array.includes(word) === true){
return true;
} else {
return false;
}
}
}

// Iteration #7: Count repetition
const wordsCount = [
'machine',
Expand All @@ -47,6 +159,18 @@ const wordsCount = [
'matter'
];


function howManyTimes (array, word){
if (array.length === 0){
return 0;
} else {
var count = 0;
array.forEach((value) => (value === word && count++)
);
return count;
}
}

// Iteration #8: Bonus

const matrix = [
Expand All @@ -71,3 +195,4 @@ const matrix = [
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];