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

Casey Abbott
Casey Abbott
7,845 Points

Why doesn't this work? I copy and pasted it into VS Code and debugged it and it works with no problems. says no function

function max (width, hight) { return max(36, 30); } if ('hight' > 'width') { alert('your ' + 'hight ' + 'is greater than your width'); } else { alert('Your ' + 'width ' + 'is greater than your hight'); }

script.js
function max (width, hight) {
  return max(36, 30);
 } if ('hight' > 'width') {
  alert('your ' + 'hight ' + 'is greater than your width'); 
 } else {
   alert('Your ' + 'width ' + 'is greater than your hight');
 } 
Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points

So first part of the challenge, it's asking you to make the max function.

function max(a, b)
{
// return A, if greater than B
// else, return B
}

Second part is asking you to wrap it in a Math.random function, then an alert function, like so:

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

2 Answers

Dillon Wyatt
Dillon Wyatt
5,990 Points

Is the goal to answer the lesson objective? I am assuming so.

First, you are returning too early. After determining which is the correct answer (the bigger number), you return only that answer.

Second, I am not sure what your alert functions are suppose to accomplish. They are not needed for the objective.

Third, you are comparing the value of two strings "height" and "weight". What you want to be comparing is the value of two integers.

So, try something like

   function max (x, y){
  if (x > y){
    return x
  }
else if (y > x) {
    return y
  };
};