Skip to content

Master #10

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Master #10

wants to merge 2 commits into from

Conversation

xonatialee
Copy link

No description provided.

xonatialee and others added 2 commits October 6, 2016 18:51
@awdriggs
Copy link
Contributor

awdriggs commented Oct 13, 2016

So all your logic looks sounds, but the way you have the functions written they will only run once. The power of functions is that they are reusable!

I'm going to use your first one as an example...copied below. Then rewrite as a reusable function.

//finds max
var numbers = [16, 25, 4, 13, 57, 88, 102, 45];

var max = numbers[0];
for(var i = 1; i < numbers.length; i++) 
{
    if(max > numbers[i]){
        max = max; 
        i++; 
}
    if(numbers[i] > max){
        max = numbers[i];
        i++;
    }
}
var printMax = function(numbers, max){
    return printMax + " max is " + numbers;
}
console.log(printMax(max));

now look at your same code, just refactored.

//finds max


var findMax = function(numArray){
  var max = numbers[0];

  for(var i = 1; i < numbers.length; i++) {
    if(max > numbers[i]){
        max = max; 
        i++; 
  } else if(numbers[i] > max){
        max = numbers[i];
        i++;
    }
  }// end for loop

  return max; //this will send the max back anywhere that findMax is executed
}

var numbers = [16, 25, 4, 13, 57, 88, 102, 45];

console.log(findMax(numbers)); //here, we'll call the function we wrote with the numbers array

In this way, we can use findMax over and over again without writing any more logic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants