29. Try it Out! - Making my brain hurt


#1

Heres my code:
// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
};

// Make a new function, olderAge, to return the age of
// the older of two people
var olderAge = function(person1, person2){
if (person1.age > person2.age){
return person1.age;
}
else if (person1.age < person2.age){
return person2.age;
}
};
// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

console.log("The older person is " + olderAge(alice, billy));

The code returns the correct answer but the error message : "Oops, try again. It looks like your olderAge function fails when both Persons have the same age. It should return the age!" Keeps appearing even though the correct answer gets printed.


#2

Your else cannot take a condition, you need to use else if.

here the fix

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

console.log(computerChoice);

if(computerChoice <= 0.33){
    computerChoice = "rock";
}else if(computerChoice <= 0.66){
    computerChoice = "paper";
}else if (computerChoice <= 1) {
    computerChoice = "scissors";
}

console.log(computerChoice);

#3

This is the code that I used that worked:

var olderAge = function(person1, person2) {
if (person1.age > person2.age) {
return person1.age;
}else if (person1.age < person2.age){
return person2.age;
}else {
return person1.age
}
};

I think your problem is that you're missing the else statement for when the ages are equal.


#4

This is the one i used
var olderAge = function(person1, person2){
if (person1.age > person2.age){

return person1.age;
}

else {

return person2.age;
}

};

Why did you used an if/else if?


#5

I just used a if/else if statement because that was how it made sense in my head: one statement for person1.age > person2.age, one statement for person1.age < person2.age, and one for person1.age = person2.age.


#6

perfect your code worked as mentioned


#7

Yes, is work perfectly! - Thanks gay.

var olderAge = function(person1, person2){

if(person1.age > person2.age){
    return person1.age;
}else{
    return person2.age;
}

}