Skip to content

Anton-Virtanen #5

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 1 commit 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
78 changes: 66 additions & 12 deletions 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;
} else if (first < second) {
return second;
} else {
return "Numbers are equal";
}
}

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

// Finding Longest Word
function findLongestWord (words) {
var maxLength = 0;

for (var i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
}
}

return maxLength;
}

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

// Calculating a Sum
function sumArray (array) {

return array.reduce((a, b) => a + b);
}

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

// Calculate the Average
function averageNumbers (array) {

return array.reduce((a, b) >= a + b)/array.length;
}

var numbers = [2, 6, 9, 10, 7, 4, 1, 9];
var average = averageNumbers(numbers);
console.log(average);

// Array of Strings
function averageWordLength (array) {
var longest = [];

function averageWordLength (words) {
for (i = 0; i <= words.length -1; i++) {
longest.push(words[i].length);
}
return longest;
}

var words = [
"seat",
"correspond",
Expand All @@ -58,7 +76,15 @@ var words = [
"fuel",
"palace"
];

var averageLength = averageWordLength(words);

function sumArray2 (averageLength) {

return averageLength.reduce((a, b) => a + b)/averageLength.length;
}
var averageLength = sumArray2 (averageLength);

console.log(averageLength);

// Unique Arrays
Expand All @@ -82,10 +108,36 @@ var words = [
var uniqued = uniquifyArray(words);
console.log(uniqued);


var words = [
"crab",
"poison",
"contagious",
"simple",
"bring",
"sharp",
"playground",
"poison",
"communion",
"simple",
"bring"
];

var uniqued = words.reduce(function(a,b){
if (a.indexOf(b) < 0 ) a.push(b);
return a;
},[]);

console.log(uniqued);

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

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

var words = [
"machine",
Expand All @@ -98,15 +150,20 @@ var words = [
"disobedience"
];

var hasMatter = doesWordExist(words, "matter");
var hasMatter = doesWordExist(words, "machine");
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 = [
Expand All @@ -126,9 +183,6 @@ var words = [
var howManyMatter = howManyTimes(words, "matter");
console.log(howManyMatter);

var howManyDog = howManyTimes(words, "dog");
console.log(howManyDog);

// Bonus Quest
function greatestProduct (matrix) {

Expand Down