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

PAR Alice & Julie #494

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
55 changes: 54 additions & 1 deletion 55 starter-code/src/functions-and-arrays.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// Find the maximum
function maxOfTwoNumbers(x,y){
if (x > y){
return x;
}else{
return y;
}
}
maxOfTwoNumbers(1,3)

// Finding Longest Word
var words = [
Expand All @@ -10,16 +18,51 @@ var words = [
'orchard',
'crackpot'
];
function findLongestWord (array) {

// Calculating a Sum
var longuestWord = ""
for (var i=0; i<array.length; i++) {
if (array[i].length > longuestWord.length){
longuestWord= array[i]
}
}

if (array.length === 0){
return
}
return longuestWord;


}
findLongestWord(words)

// Calculating a Sum
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
function sumArray (array){
var sum = 0;
for (i=0; i<array.length; i++){
sum = sum + array[i];
}
return sum
}
sumArray(numbers)

// Calculate the Average

var numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
function averageNumbers (array){
var sumAverage = 0;
for (i=0; i<array.length; i++){
sumAverage = sumAverage + array[i];
var average = sumAverage/array.length
}
return average
}

Choose a reason for hiding this comment

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

To keep your code dry (= avoid repetitions), you can also call the sumArray function that you created earlier.

function averageNumbers(arrayNumber) {
   var sumRes = sumArray(arrayNumber);
   if (arrayNumber.length > 0) {
   var average = sumRes / arrayNumber.length; 
   }
   return average;
 }

Choose a reason for hiding this comment

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

the if statement here ( if (arrayNumber.length > 0) ) is to handle the case when we try to pass an empty array to the function, to please Jasmine :)

averageNumbers(numbersAvg)

// Array of Strings


var wordsArr = [
'seat',
'correspond',
Expand All @@ -32,6 +75,16 @@ var wordsArr = [
'fuel',
'palace'
];
function averageWordLength (array){
var wordLength = 0
for (i=0; i<array.length; i++){
wordLength += array[i].length;

}
var averageLength= wordLength/array.length;
return averageLength
}
averageWordLength(wordsArr)

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