Skip to content
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
95 changes: 70 additions & 25 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,68 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


function maxOfTwoNumbers(number1, number2) {
if (number1 > number2) {
return number1;
} else {
return number2;
}
}

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

function findLongestWord() {}


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

let longestword = '';
for (let i = 0; i < wordarray.length; i++) {
if (wordarray[i].length > longestword.length) {
longestword = wordarray[i];
}
}
return longestword;
}

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

function sumNumbers() {}


function sumNumbers(numbersarray) {
let result = 0;
for (let i = 0; i < numbersarray.length; i++) {
result = result + numbersarray[i];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also write this as result += numbersarray[i]; but this is just a tiny detail.

}
return result;
}

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



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

function averageNumbers() {}

function averageNumbers(numbersarray) {
if (numbersarray.length === 0) {
return null;
}
let sum = sumNumbers (numbersarray)
let numberOfElements= numbersarray.length
let average = sum / numberOfElements
return average
Comment on lines +49 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another tiny detail. Just so that you know, you could do this in one line return sumNumbers(numbersarray) / numbersarray.length

}

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

function averageWordLength() { }
function averageWordLength(wordsarray) {
let wordlist = []
for (let i = 0; i < wordsarray.length; i++){
wordlist.push(wordsarray[i].length)
}
let average = averageNumbers(wordlist)
return average
}
Comment on lines +58 to +65

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works fine but it creates an extra array. Since the length is just a number we can add all the lengths in a variable instead. But the way you did it also works.

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


// Bonus - Iteration #4.1
function avg() {}
Expand All @@ -52,16 +82,25 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}

function uniquifyArray(wordsUniqueArray) {

}
Comment on lines +85 to +87

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the lab solution for this one and feel free to reach out if you have any questions.


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

function doesWordExist() {}


function doesWordExist(wordsFindArray,word) {
if (wordsFindArray.length === 0) {
return null;
}
let found = false
for (let i = 0; i < wordsFindArray.length; i++){
if (wordsFindArray [i] === word){
found = true
}
}
return found
}
Comment on lines +92 to +103

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works but if the target word is the first word in the array and the array has 1000 elements, it will still loop on the entire array before returning true. Instead you could directly return true in line 99 so the function ends and return true as soon as the target is found. If the for loop is finished, then this means the target was not found and you can return false.


// Iteration #7: Count repetition
const wordsCount = [
Expand All @@ -78,9 +117,18 @@ const wordsCount = [
'matter'
];

function howManyTimes() {}


function howManyTimes(wordsCountArray, word) {
let result = 0
if (wordsCountArray.length === 0) {
return 0;
}
for (let i = 0; i < wordsCountArray.length; i++){
if ( wordsCountArray [i] === word) {
result++
}
}
return result
}

// Iteration #8: Bonus
const matrix = [
Expand Down Expand Up @@ -108,9 +156,6 @@ const matrix = [

function greatestProduct() {}




// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
Expand Down