卡在 5.3.4 codeHS 程序 (JavaScript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48712657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stuck on 5.3.4 codeHS program (JavaScript)
提问by Alyssa
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.
编写一个在图形画布上绘制水平线的函数。如果一条线是水平线,则端点的 y 值相同。您的函数的参数应该是 y 位置和长度,并且您的所有线条都应该从 x 位置 0 开始。您的函数必须命名为水平线。
For example if you call:
例如,如果您调用:
horizontalLine(100, 200);
you should get a horizontal line of length 200 starting at position (0, 100).
你应该得到一条长度为 200 的水平线,从位置 (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.
结局世界应该是这样的。现在我已经让我的世界看起来完全一样,但是代码检查返回错误。我不确定我的代码有什么问题,但我猜是因为它希望我在 start 函数中输入两个数字而不是四个数字,但我无法让它工作。我已经被困在这个问题上近一个月了,答案可能很简单,但我想不通,请帮忙。
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);
}
采纳答案by tmdesigned
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.
是的,自动代码检查器取决于您的功能是否按照指定的方式设置。可能希望您的函数采用 2 个参数,但您的函数需要 4 个,因此当它发送值时,它们没有分配给正确的变量。
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.
查看您在开始函数调用水平线中的值。您是否注意到在给定的 4 个中,2 个每次总是相同的?您的第 2 个和第 4 个参数始终相同,因为正如练习提示所述,平线具有相同的 y 值。所以你不需要单独传递它们,因为它们是相同的。您只需传入一次,但使用两次。
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.
提示还指定 x 值将始终从 0 开始。然而,您每次都将其传入(注意您的第一个参数始终为 0?)。再说一次,您不需要传入它,只需使用它即可。
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!
函数的前两行实际上或多或少地在做他们需要做的事情。他们正在设置不需要传入的变量。那么,你为什么要传入它们?只需更改您的函数定义,只发送您实际使用的两个参数即可!

