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

lab-javascript-functions-and-arrays2 #4180

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
58 changes: 51 additions & 7 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}
function maxOfTwoNumbers(num1,num2) {
if (num1 > num2) {
return num1;
}
else {
return num2;
}
}



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

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

for ( let i = 0; i < words.length; i++) {
if (word.length < words[i].length) {
word = words[i]
}
}
return word
}
console.log(findLongestWord(words))




// Iteration #3: Calculate the sum
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];

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


}
return total
}



// Iteration #3.1 Bonus:
// Iteration #3.2 Bonus:
//Iteration #3.1: Sum numbers
function sum() {}


Expand All @@ -26,7 +56,22 @@ function sum() {}
// Level 1: Array of numbers
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];

function averageNumbers() {}
function averageNumbers(numbers) {

if (numbers.length === 0) {
return = null;
}
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
let average = (sum/numbers.length);
return average;

}





// Level 2: Array of strings
Expand Down Expand Up @@ -111,8 +156,7 @@ function greatestProduct() {}



// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */

if (typeof module !== 'undefined') {
module.exports = {
maxOfTwoNumbers,
Expand Down