简体   繁体   English

我正在尝试在 class 的帮助下打印已定义多边形的周长,但它给了我未定义为 output 请告诉我问题出在哪里

[英]I am trying to print perimeter of defined polygons with the help of class but it is giving me undefined as an output please tell me where the problem

I am getting undefined as output please tell me to resolve this and tell me where the problem is.我越来越未定义为 output 请告诉我解决这个问题并告诉我问题出在哪里。

 class Polygon { constructor(side) { this.values = side; } perimeter() { var x; for (var i = 0; i > this.values.length; i++) { x = values[i]; x += values[i]; } return x; } } var triangle = new Polygon([3, 4, 5]); const rectangle = new Polygon([10, 20, 10, 20]); const square = new Polygon([10, 10, 10, 10]); const pentagon = new Polygon([10, 20, 30, 40, 43]); console.log(rectangle.perimeter()); console.log(square.perimeter()); console.log(pentagon.perimeter());

You have bugs in you code.您的代码中有错误。 Check this code and debug properly.检查此代码并正确调试。

...
    perimeter(){ 
        var x = 0; // Initialize x to zero when begin
        for (var i = 0 ; i < this.values.length; i++){ // change greaeter than ( > ) symbol to less than symbol ( < )
            // x = values[i]; // remove this line
            x += this.values[i]; // put this keyword to infront
        }
        return x;
    }
}
...

You have to use this keyword while using the variable declared in the constructor and for adding values in x you have to assign 0 to x and i will be less than the length of the array else it will return just 0. Lastly you have to remove this line x = values[i] because it will reassign the value of x on each iteration of the loop.您必须在使用构造函数中声明的变量时使用此关键字,并且要在 x 中添加值,您必须将 0 分配给 x 并且 i 将小于数组的长度,否则它将仅返回 0。最后您必须删除这行 x = values[i] 因为它将在循环的每次迭代中重新分配 x 的值。

class Polygon {
  constructor(side) {
    this.values = side;
  }
  perimeter() {
    var x = 0;
    for (var i = 0; i < this.values.length; i++) {
      x += this.values[i];
    }
    return x;
  }
}
var triangle = new Polygon([3, 4, 5]);

const rectangle = new Polygon([10, 20, 10, 20]);
const square = new Polygon([10, 10, 10, 10]);
const pentagon = new Polygon([10, 20, 30, 40, 43]);

console.log(rectangle.perimeter());
console.log(square.perimeter());
console.log(pentagon.perimeter());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在运行这个 javascript 代码,但它给了我一个..错误请告诉我出了什么问题,因为我认为代码是正确的? - I am running this javascript code but it is giving me an.. error please tell me what is wrong because I think the code is correct? 请告诉我我在下面的转换为float的javascript代码中做错了什么 - please tell me where i am doing mistake in following javascript code of conversion to float 我正在尝试制作 Jquery 模态弹出对话框。 请帮我 - I am trying to make a Jquery Modal Popup dialogue. Please help me AJAX更新面板不起作用。 请帮助我找出我在哪里错...? - AJAX Update Panel Doesn't Work. Please help me finding where am I wrong…? 请帮我解决这个问题我试图解决范围提取 - Please Help me solve this problem i have trying to solve Range extraction 有人可以告诉我哪里出错了吗? - Can someone please tell me where I went wrong? 我正在尝试从 useEffect 挂钩调用 function 并且它给了我未定义 - I am trying to call a function from useEffect hook and it's giving me undefined 我是 JavaScript 的新手。 请帮我进行 javascript 验证 - I am new to JavaScript. please help me for javascript validation 我正在尝试在 typescript 中使用 multer 我想上传一个文件但我的身体是空的 请告诉我如何修复它 - i am trying to use multer in typescript i want to upload a file but my body is empty please tell me how to fix it javascript我怎么解决这个问题,请帮帮我 - How can i solve this problem in javascript, please help me
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM