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

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Binary file added BIN +6 KB .DS_Store
Binary file not shown.
59 changes: 51 additions & 8 deletions 59 starter-code/functions-and-arrays.js
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");
Copy link
Contributor

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.

}
}

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++){
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = [
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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];
Expand All @@ -34,7 +58,11 @@ console.log(total);

// Calculate the Average
function averageNumbers (array) {

var totalSum=0;
array.forEach(function(number){
Copy link
Contributor

Choose a reason for hiding this comment

The 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];
Expand All @@ -43,9 +71,14 @@ console.log(average);

// Array of Strings
function averageWordLength (array) {

var total=0;
array.forEach(function(number){
Copy link
Contributor

Choose a reason for hiding this comment

The 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",
Expand All @@ -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",
Expand All @@ -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){
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = [
Expand Down
22 changes: 22 additions & 0 deletions 22 starter-code/words.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.