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

Solved LAb #420

Open
wants to merge 2 commits 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
64 changes: 59 additions & 5 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,88 @@
// Iteration 1 | Find the Maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(a, b) {
if (a > b) {
return a;
} else {
return b;
}
}




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

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

let longestWord = words[0];

for (let i = 1; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}

return longestWord;  

}




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

function sumNumbers() {}
function sumNumbers(numbers) {
let sum = 0;

for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}

return sum;
}
const totalSum = sumNumbers(numbers);
console.log(totalSum);


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

function averageNumbers() {}
function averageNumbers(numbers) {
if (numbers.length === 0) {
return 0;
}

let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}

// Calculate the average
const average = sum / numbers.length;

return average;
}



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

function doesWordExist() {}
function doesWordExist(words, wordToSearch) {
if (words.length === 0) {
return null;
}

for (let i = 0; i < words.length; i++) {
if (words[i] === wordToSearch) {
return true;
}
}

return false;
}