-
Notifications
You must be signed in to change notification settings - Fork 6.7k
BCN - Desiree Lucas Cristian #151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
// Find the maximum | ||
function maxOfTwoNumbers (first, second) { | ||
|
||
if (first>second){ | ||
console.log ("first"); | ||
} | ||
else if (second>first){ | ||
console.log ("second"); | ||
} | ||
else { | ||
console.log("the numbers are the same"); | ||
} | ||
} | ||
|
||
var largest = maxOfTwoNumbers(2, 6); | ||
console.log(largest); | ||
|
||
// Finding Longest Word | ||
function findLongestWord (words) { | ||
|
||
function findLongestWord (words) { | ||
var biggest=""; | ||
for ( var ix=0; ix < words.length; ix++){ | ||
if ( biggest.length < words[ix].length){ | ||
biggest=words[ix]; | ||
} | ||
} | ||
for ( var j=0; j<words.lenght; j++){ | ||
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 don't need this second for. I understand you use it to display more than one result (if there is a tie for the longest word), but the problem again is that you MUST return the result instead of showing it in the console. In this case, if you want to display more than one word, you will need an array. If the function's signature is expecting an output of a value, returning an array could be a problem. |
||
if (biggest.length==words[j].length){ | ||
console.log (words[j]); | ||
} | ||
} | ||
console.log(biggest); | ||
} | ||
|
||
var words = [ | ||
|
@@ -20,12 +40,16 @@ var words = [ | |
"orchard", | ||
"crackpot" | ||
]; | ||
|
||
var longest = findLongestWord(words); | ||
console.log(longest); | ||
|
||
// Calculating a Sum | ||
function sumArray (array) { | ||
|
||
var totalSum=0; | ||
array.forEach(function(number){ | ||
totalSum+=number; | ||
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. Good job with the forEach. However, you have forgotten return something. |
||
}); | ||
} | ||
|
||
var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]; | ||
|
@@ -34,7 +58,11 @@ console.log(total); | |
|
||
// Calculate the Average | ||
function averageNumbers (array) { | ||
|
||
var totalSum=0; | ||
array.forEach(function(number){ | ||
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. Do you have another function that sums all the values in an array? Then, maybe you can use it here instead of repeating code. |
||
totalSum+=number; | ||
}); | ||
return totalSum/array.length; | ||
} | ||
|
||
var numbers = [2, 6, 9, 10, 7, 4, 1, 9]; | ||
|
@@ -43,9 +71,14 @@ console.log(average); | |
|
||
// Array of Strings | ||
function averageWordLength (array) { | ||
|
||
var total=0; | ||
array.forEach(function(number){ | ||
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. Good job. However, be careful with variable's names. Don't call it number if it is a word. |
||
total+=number.length; | ||
}); | ||
return total/array.length; | ||
} | ||
|
||
|
||
var words = [ | ||
"seat", | ||
"correspond", | ||
|
@@ -63,9 +96,14 @@ console.log(averageLength); | |
|
||
// Unique Arrays | ||
function uniquifyArray (array) { | ||
|
||
var newarray=[]; | ||
for ( var ix=0; ix < array.length; ix++){ | ||
if ( newarray.indexOf(array[ix])===-1 ){ | ||
newarray.push(array[ix]); | ||
} | ||
} | ||
return newarray; | ||
} | ||
|
||
var words = [ | ||
"crab", | ||
"poison", | ||
|
@@ -84,7 +122,12 @@ console.log(uniqued); | |
|
||
// Finding Elements | ||
function doesWordExist (wordsArray, word) { | ||
|
||
var biggest=""; | ||
for ( var ix=0; ix < words.length; ix++){ | ||
if ( biggest.length < words[ix].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. You don't want to find the biggest word, you want to check if that word is in the array. |
||
biggest=words[ix]; | ||
} | ||
} | ||
} | ||
|
||
var words = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<script> | ||
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. What is this? Why are you including a script in another html file? |
||
function findLongestWord (words) { | ||
var LongestWord=""; | ||
words.forEach(function(word){ | ||
if(word.lenght>longestWord.lenght){ | ||
longestWord = word; | ||
} | ||
}); | ||
return longestWord; | ||
} | ||
|
||
|
||
var words = [ | ||
"mystery", | ||
"brother", | ||
"aviator", | ||
"crocodile", | ||
"pearl", | ||
"orchard", | ||
"crackpot" | ||
]; | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are showing the result in the console instead of returning it. That way, the variable 'largest' in line 14 is going to be undefined (because you are not returning anything). Usually you want your functions to return something, that way you can use that value ahead.