9. function with two parameters(fixed)


#1

I am 90% sure this correct but codeacademy does not. could you check it out for me?

// Write your function starting on line 3

var perimeterBox = function(length, length, width, width) {
return length + length + width + width;
};
var newNumber = perimeterBox(10,10,5,5)
console.log(newNumber);

then I get this error
Oops, try again. It looks like your perimeterBox function doesn't return the correct perimeter (4) for a width of 1 and a length of 1.


#2

The problem is here:

var perimeterBox = function(length, length, width, width) {

here you use the length and width parameter doubled. Think of what this would do when it would be possible. This means that by perimeterBox(10,9,5,4) you would be able to could have 2 variable with the same name but different values in the same scope. How should a human or a computer be able to sort out when you mean the first length parameter and when you mean the second one? The answer is simple: it is impossible. And the solution is simple as well: Why do you want to double the parameter? Why don't just use the existing parameter 2 times?

    var perimeterBox = function(length, width) {
    return length + length + width + width;
    };
    var newNumber = perimeterBox(10,5)

This already does the job.


#3

// Write your function starting on line 3
//try this.....
var perimeterBox=function(length, width){
return length*2 + width * 2;
};

perimeterBox(2,2);

#4

var perimeterBox = function (length, width) {
return (length *2) + (width * 2);
}
perimeterBox(10,5);

This works. Senks for priviosly replies - it`s helply : )


#5

Hi haxor789!! i just did the same what you have shared above. literally the same and its showing error like your parameterBox is not defined. help me with this.


#6

It's called "perimeterBox" not "parameterBox" maybe that is already the problem otherwise copy and paste your code. If you think your code is 100% correct and maybe it is press F5 or whatever refreshes your page and if the error persists than it is probably a real one. Also the reference errors are thrown by the interpreter not by the testing scripts of the author so they are probably real.


#7

A post was split to a new topic: What is the difference