Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

anthony-bruscantini #4

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

Closed
wants to merge 2 commits into from
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
70 changes: 61 additions & 9 deletions 70 starter-code/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Find the maximum
function maxOfTwoNumbers (first, second) {

if (first > second) return first;
Copy link

Choose a reason for hiding this comment

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

Good job!!

Also, you could consider this:

return (first > second ? first : second);

else return second;
}

var largest = maxOfTwoNumbers(2, 6);
console.log(largest);



// Finding Longest Word
function findLongestWord (words) {

var longestWordIndex = 0;
var longestWordlen = words[0].length;

for (var i = 1; i < words.length; ++i){
var curWordlen = words[i].length;
if (curWordlen > longestWordlen){
longestWordIndex = i;
longestWordlen = curWordlen;
}
}

return words[longestWordIndex];
}

var words = [
Expand All @@ -25,7 +39,9 @@ console.log(longest);

// Calculating a Sum
function sumArray (array) {

return array.reduce(function (accumulator, currentValue){
return accumulator + currentValue;
}, 0);
}

var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
Expand All @@ -34,7 +50,9 @@ console.log(total);

// Calculate the Average
function averageNumbers (array) {

var sum = sumArray(array);
var arrayLen = array.length;
return sum/arrayLen;
}

var numbers = [2, 6, 9, 10, 7, 4, 1, 9];
Expand All @@ -43,7 +61,10 @@ console.log(average);

// Array of Strings
function averageWordLength (array) {

var totalLengthofWords = array.reduce(function (a, b){
return a + b.length;
}, 0);
return totalLengthofWords/array.length;
}

var words = [
Expand All @@ -63,7 +84,13 @@ console.log(averageLength);

// Unique Arrays
function uniquifyArray (array) {

var uniqued = [];
array.forEach(function (currentValue, index){
if (array.indexOf(currentValue) === index){
uniqued.push(currentValue);
}
});
return uniqued;
}

var words = [
Expand All @@ -84,7 +111,14 @@ console.log(uniqued);

// Finding Elements
function doesWordExist (wordsArray, word) {

var i = 0;
while(i < wordsArray.length){
if (wordsArray[i] === word){
return true;
}
++i;
}
return false;
}

var words = [
Expand All @@ -106,7 +140,13 @@ console.log(hasDog);

// Counting Repetion
function howManyTimes (words, word) {

var i = 0, count = 0;
while(i < words.length){
if (words[i] === word)
++count;
++i;
}
return count;
}

var words = [
Expand All @@ -131,7 +171,19 @@ console.log(howManyDog);

// Bonus Quest
function greatestProduct (matrix) {

var result = 1;
for(var i = 1; i < matrix.length - 1; ++i){
for(var j = 1; j < matrix[i].length - 1; ++j){
var curProduct = 1;
//get product right
curProduct *= matrix[i - 1][j];
curProduct *= matrix[i][j + 1];
curProduct *= matrix[i + 1][j];
curProduct *= matrix[i][j - 1];
result = curProduct > result ? curProduct : result;
}
}
return result;
}

var matrix = [
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.