Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  80] [ 3]  / answers: 1 / hits: 21501  / 6 Years ago, fri, february 9, 2018, 12:00:00

In the lesson the instructions are to:



Write a function that draws horizontal lines on the graphics canvas. If a line is horizontal, then the y-values for the endpoints are the same. The parameters to your function should be the y location, and the length, and all of your lines should start at x position 0. Your function must be named horizontalLine.



For example if you call:



horizontalLine(100, 200);


you should get a horizontal line of length 200 starting at position (0, 100).



If your start function looks like:



function start(){
horizontalLine(100, 200);
horizontalLine(200, 100);
horizontalLine(300, 20);
}


The ending world should look like this. Now I've gotten my world to look exactly like that but the code check returns wrong. I'm not sure whats wrong with my code but I'm guessing its because it wants me to input two numbers instead of the four in my start function but I can't get it to work. I've been stuck on this for nearly a month and the answer is probably really simple but I can't figure it out, please help.



This is my code:



function start() {
horizontalLine(0, 100, 200, 100);
horizontalLine(0, 200, 100, 200);
horizontalLine(0, 300, 20, 300);
}

function horizontalLine (x1, y1, x2, y2) {
var x1 = 0;
var y1 = y2;
var line = new Line (x1, y1, x2, y2);
add(line);
}

More From » parameters

 Answers
5

Yes, automated code checkers depend on things like your function being set up just as specified. It is likely expecting your function to take 2 parameters, but yours wants 4, so when it sends in the values they are not getting assigned to the right variables.



Look at the values you have in your start function calls to horizontalLine. Do you notice that of the 4 given, 2 are always the same each time? Your 2nd and 4th argument are always the same, because, as the exercise prompt explains, flat lines have the same y value. So you don't need to pass them in separately because they are the same. You'll just pass it in once, but use it twice.



The prompt also specifies that the x value will always start at 0. Yet, you are passing it in every time (notice that your first parameter is always 0?). So again, you don't need to pass that in, just use it.



That's the concept, now for the implementation.



The first two lines of your function are actually doing, more or less, what they need to do. They are setting up the variables that DON'T need to be passed in. So, why are you passing them in? Just change your function definition to only send in the two parameters you're actually using!


[#55204] Wednesday, February 7, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ulysses

Total Points: 111
Total Questions: 118
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;