- Notifications
You must be signed in to change notification settings - Fork6.7k
MIA Hannah Loop#447
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.
MIA Hannah Loop#447
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Find the Maximum | ||
function maxOfTwoNumbers (num1, num2) { | ||
if (num1 > num2) { | ||
console.log('The maximum number is ' + num1); | ||
} else if (num1 < num2) { | ||
console.log('The maximum number is ' + num2); | ||
} else { | ||
console.log('They\'re the same!'); | ||
} | ||
} | ||
maxOfTwoNumbers (prompt('Type your first number'), (prompt('Type your second number'))); | ||
// Find The Largest Word | ||
var words = [ | ||
'mystery', | ||
'brother', | ||
'aviator', | ||
'crocodile', | ||
'pearl', | ||
'orchard', | ||
'crackpot' | ||
]; | ||
function findLongestWord(wordsArray) { | ||
var longestWord = ''; | ||
wordsArray.forEach(function(eachWord){ | ||
if(eachWord.length > longestWord.length) { | ||
longestWord = eachWord; | ||
} | ||
}); | ||
return longestWord; | ||
} | ||
console.log (findLongestWord(words)); | ||
// Calculating a Sum | ||
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10].reduce(function (accumulator, currentValue) { | ||
return accumulator + currentValue; | ||
}, 0); | ||
console.log(numbers); | ||
// Example 2 | ||
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
function calculateSum(arrayOfNumbers) { | ||
var sum = 0; | ||
arrayOfNumbers.forEach(function(oneParticularNumber) { | ||
sum += oneParticularNumber | ||
}); | ||
return sum; | ||
} | ||
console.log(calculateSum(numbers)); | ||
// Calculate the Average | ||
function calculateAverage(numberArray) { | ||
return (calculateSum(numberArray) / numberArray.length) | ||
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. Reusing previously defined functions is a smart move. Good call! | ||
} | ||
console.log(calculateAverage()) | ||
// Unique Arrays | ||
var wordsWithDuplicates = [ | ||
'crab', | ||
'poison', | ||
'contagious', | ||
'simple', | ||
'bring', | ||
'sharp', | ||
'playground', | ||
'poison', | ||
'communion', | ||
'simple', | ||
'bring' | ||
]; | ||
function uniquifyArray(theArray) { | ||
if(theArray.length < 1) { | ||
return undefined; | ||
} | ||
var newArray = []; | ||
wordsWithDuplicates.forEach(function(eachWord) { | ||
if(newArray.indexOf(eachWord) === -1) { | ||
newArray.push(eachWord) | ||
} | ||
}); | ||
return newArray | ||
console.log(uniquifyArray(wordsWithDuplicates)); | ||
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. Thisconsole.log() has to be outside of this} braces. | ||
} | ||
// Finding Elements | ||
function doesItExist(arrayOfWords, wordToSearchFor) { | ||
var doesIt = false; | ||
arrayOfWords.forEach(function(eachWord) { | ||
if(eachWord === wordToSearchFor) { | ||
doesIt = true | ||
} | ||
}); | ||
return doesIt | ||
} | ||
console.log(doesItExist(wordsWithDuplicates, 'crab')); //change last string to test loop | ||
// Counting Repetition | ||
function numberOfTimes(theArray, word) { | ||
var count = 0; | ||
theArray.forEach(function(eachWord) { | ||
if(eachWord === theWord) { | ||
count++ | ||
} | ||
}); | ||
return count; | ||
} | ||
console.log(numberOfTimes(wordsWithDuplicates, 'rain')); |
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.