Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

BCN - Linda & Santiago#215

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

Closed
Closed
Changes fromall 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
26 changes: 26 additions & 0 deletionsstarter-code/src/functions-and-arrays.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
// Find the maximum

function maxOfTwoNumbers (item1, item2) {
if (item1>item2) {
return item1;
}
else {
return item2;
}
}


// Finding Longest Word
var words = [
'mystery',
Expand All@@ -11,6 +21,22 @@ var words = [
'crackpot'
];

function findLongestWord (box){
var longest = "";
Copy link
Contributor

Choose a reason for hiding this comment

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

This function will fail returning undefined when the array is empty. You can solve this checking if the array is empty and returning undefined then. A better way is asign to your comparator (longest) the first array's element. If the array is empty, longest will be 'undefined' and the function will return 'undefined'. Using an internal element for comparision solve some tricky problems (e.g. finding a minimum).

function findLongestWord(box) {  var longest = box[0];  for (var i = 1; i < box.length; i++) {    if (box[i].length > longest.length) {      longest = box[i];    }  }  return longest;}


for (var i=0; i < box.length; i++) {
if (box[i].length > longest.length) {
var longest = box[i];
} else {

}
}
return longest;
}




// Calculating a Sum

var numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp