Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

I'm just not getting it. Please, help.

I have tried this several different ways. Every time, my code seems to post to html like comments. I have check it and re-check it. At this point, I think I am over thinking it. Any advise it greatly welcome.

script.js
function max(upper, lower) {
  if (field.value === 25) {
  return true;
}  else {
  return false;
  }
  var upper = 25;
    var lower = 15;
    var field = (upper) < (lower);
if field.value = 25 {
        document.write(upper);
    } else
        document. write("wrong method);

2 Answers

Steven Ang
Steven Ang
41,751 Points

Woah, you went way off topic! The task didn't tell you to use document.write to the html file or does it tell you to use field.value to find the value.

First, the task asks you to create a function named max() and pass two parameters. In this case, an upper value and a lower value. Like this:

function max(a, b) { }

Afterwards, you define a set of conditional statement in the function scope. Then, you compare which of the numbers are larger and return the larger number. Like this:

function max(a, b) {
 if (a > b) {
   return a;
 } else {
   return b;
 }
}

Lastly, you call the function in the Matn.random() function which nested inside the alert box. You can give any numbers you like as your arguments. Like this:

function max(a, b) {
 if (a > b) {
   return a;
 } else {
   return b;
 }
}

alert( Math.random( max(1, 2) ) );

Once you've done, your final code should or similar to the code above.

Hi Kelly

function max(number1, number2) { //Create a new function named max which accepts two numbers as arguments
  if (number1 > number2) { //The function should return the larger of the two numbers.
    return number1; 
  } else {
    return number2; 
  }
};
alert(max(24, 106)); //call function max with two numbers and display the results in an alert dialog.