Skip to content
Open

Done #157

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
103 changes: 81 additions & 22 deletions src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,74 @@
// Iteration #1: Find the maximum
function maxOfTwoNumbers() {}


if (a > b) {
return a;
}
else {
return b;
}
console.log(maxOfTwoNumbers(2, 3));

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

function findLongestWord() {}


function findLongestWord() {
let longestWord = '';
for (let i = 0; i < words.length; i++) {
if (words[i].length > longestWord.length) {
longestWord = words[i];
}
}
return longestWord;
}
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() {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
console.log(sumNumbers(numbers));

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

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() {
for (let i = 0; i < numbersAvg.length; ){
sum += numbersAvg[i];
i++;
}
}
console.log(averageNumbers(numbersAvg));


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

function averageWordLength() { }

function averageWordLength() {
for (let i = 0; i < wordsArr.length; i++) {
sum += wordsArr[i].length;
}
return sum / wordsArr.length;
}
console.log(averageWordLength(wordsArr));s
// Bonus - Iteration #4.1
function avg() {}

// Iteration #5: Unique arrays
const wordsUnique = [
'crab',
'poison',
'poison',
'contagious',
'simple',
'bring',
Expand All @@ -52,16 +80,30 @@ const wordsUnique = [
'bring'
];

function uniquifyArray() {}


function uniquifyArray() {
let uniqueWords = [];
for (let i = 0; i < wordsUnique.length; i++) {
if (!uniqueWords.includes(wordsUnique[i])) {
uniqueWords.push(wordsUnique[i]);
}
}
return uniqueWords;
}
console.log(uniquifyArray(wordsUnique));

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

function doesWordExist() {}

function doesWordExist() {
for (let i = 0; i < wordsFind.length; i++) {
if (wordsFind[i] === word) {
return true;
}
}
return false;
}

console.log(doesWordExist(wordsFind, 'matter'));

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

function howManyTimes() {}
function howManyTimes() {
let count = 0;
for (let i = 0; i < wordsCount.length; i++) {
if (wordsCount[i] === word) {
count++;
}
}
return count;
}
console.log(howManyTimes(wordsCount, 'matter'));



Expand Down Expand Up @@ -106,9 +157,17 @@ const matrix = [
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
];

function greatestProduct() {}

function greatestProduct() {
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}

}
console.log(greatestProduct(matrix));


// The following is required to make unit tests work.
Expand Down