Skip to content
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

Marian Update functions-and-arrays.js #425

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
61 changes: 55 additions & 6 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,83 @@
// Iteration 1 | Find the Maximum
function maxOfTwoNumbers() {}

function maxOfTwoNumbers(num1, num2) {
if (num1 > num2) {
return num1;
} else if (num2 > num1) {
return num2;
} else {
return num2;
}
}
console.log(maxOfTwoNumbers(3, 7));



// Iteration 2 | Find the Longest Word
const words = ["mystery", "brother", "aviator", "crocodile", "pearl", "orchard", "crackpot"];

function findLongestWord() {}
function findLongestWord(array) {
if (array.length === 0){
return null;
}
else {
let word = "";
for (let i = 0; i < array.length; i++) {
if (word.length < array[i].length) {
word = array[i];
}
}
return word;
}

function longestStringReduce(array) {
return array.reduce((a, b) => a.length < b.length ? b : a, "");
}

}


console.log(findLongestWord(words));




// Iteration 3 | Sum Numbers
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

function sumNumbers() {}
function sumNumbers(array1) {
sum = 0;
array1.forEach((element) => {
sum +=element;
});
return sum;
}




// Iteration 4 | Numbers Average
const numbers2 = [2, 6, 9, 10, 7, 4, 1, 9];

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


console.log(averageNumbers(numbers2));



// Iteration 5 | Find Elements
const words2 = ["machine", "subset", "trouble", "starting", "matter", "eating", "truth", "disobedience"];

function doesWordExist() {}
function doesWordExist(haystack) {
if (haystack.length === 0)
return null;
}