- Notifications
You must be signed in to change notification settings - Fork6.7k
stuart-gardner, thomas-beguin#117
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
/* | ||
// Find the maximum | ||
function maxOfTwoNumbers (first, second) { | ||
if(first>second){ | ||
return first; | ||
} else { | ||
return second; | ||
} | ||
} | ||
var largest = maxOfTwoNumbers(8, 6); | ||
console.log(largest); | ||
// Finding Longest Word | ||
var longestWord = ""; | ||
function findLongestWord (words) { | ||
for(var i=0; i<words.length; i++){ | ||
if(words[i].length > longestWord.length){ | ||
longestWord = words[i]; | ||
} | ||
} | ||
return longestWord; | ||
} | ||
var words = [ | ||
@@ -22,28 +38,50 @@ var words = [ | ||
]; | ||
var longest = findLongestWord(words); | ||
console.log(longest); | ||
console.log(longest.length); | ||
// Calculating a Sum | ||
function sumArray (array) { | ||
var sum = 0; | ||
for (var i=0; i < numbers.length;i++){ | ||
sum += numbers[i]; | ||
} | ||
return sum; | ||
} | ||
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
var total = sumArray(numbers); | ||
console.log(total); | ||
// Calculate the Average | ||
function averageNumbers (array) { | ||
return sumArray(numbers)/array.length; | ||
} | ||
var numbers = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
var average = averageNumbers(numbers); | ||
console.log(average); | ||
// Array of Strings | ||
var numbers = []; | ||
function averageWordLength (array) { | ||
for (var i=0;i<words.length;i++) { | ||
numbers.push(words[i].length); | ||
} | ||
//console.log(numbers); | ||
//console.log(words.length); | ||
return averageNumbers(numbers); | ||
} | ||
var words = [ | ||
@@ -61,9 +99,26 @@ var words = [ | ||
var averageLength = averageWordLength(words); | ||
console.log(averageLength); | ||
// ABOVE ALL SEEMS TO BE CORRECT | ||
// Unique Arrays | ||
function uniquifyArray (array) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You are trying to remove duplicates from the array. This approach is very hard. An easier one would be to create a new array in which you add elements you haven't seen before. | ||
for(var i=0; i<words.length; i++){ | ||
for(var j = i+1; j<words.length; j++){ | ||
if(words[i] === words[j]){ | ||
words.splice(j,1); | ||
} | ||
} | ||
} | ||
return words | ||
} | ||
var words = [ | ||
@@ -82,9 +137,20 @@ var words = [ | ||
var uniqued = uniquifyArray(words); | ||
console.log(uniqued); | ||
// DEDUPLICATING WORKS | ||
// Finding Elements | ||
function doesWordExist (wordsArray, word) { | ||
for (var i=0;i<wordsArray.length;i++){ | ||
if (word===wordsArray[i]){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
var words = [ | ||
@@ -104,11 +170,19 @@ console.log(hasMatter); | ||
var hasDog = doesWordExist(words, "dog"); | ||
console.log(hasDog); | ||
// Counting Repetion | ||
function howManyTimes (words, word) { | ||
var count = 0; | ||
for (var i=0;i<words.length;i++) { | ||
if(words[i]===word){ | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
var words = [ | ||
"machine", | ||
"matter", | ||
@@ -129,9 +203,39 @@ console.log(howManyMatter); | ||
var howManyDog = howManyTimes(words, "dog"); | ||
console.log(howManyDog); | ||
*/ | ||
// Bonus Quest | ||
var biggestNumber = 0; | ||
function greatestProduct (matrix) { | ||
//i is rows | ||
for(var i=1 ; i<matrix.length-1 ; i++ ){ | ||
for(var j=1; j<matrix[i].length-1; i++){ | ||
var product = matrix[i][j] * | ||
matrix[i][j-1] * | ||
matrix[i][j+1] * | ||
matrix[i-1][j] * | ||
matrix[i+1][j]; | ||
if(product > biggestNumber){ | ||
biggestNumber = product; | ||
} | ||
} | ||
} | ||
return biggestNumber; | ||
} | ||
var matrix = [ | ||
@@ -157,5 +261,7 @@ var matrix = [ | ||
[01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48], | ||
]; | ||
//console.log(matrix[6+1][8-1]); | ||
var maxProduct = greatestProduct(matrix); | ||
console.log(maxProduct); |