首页 > 解决方案 > 如何编写一个函数,返回一个宽为 5,高为 8 的矩形的周长?

问题描述

我目前的尝试如下,但我什至不确定将 5 和 8 的输入放在哪里。任何帮助/建议都会受到赞赏。

function rectPerimeter(width, height) {
  return 2 * width + 2 * height;
}

console.log(rectPerimeter)

标签: javascriptfunction

解决方案


执行以下操作:

function rectPerimeter(width, height) {
  return 2 * width + 2 * height;
}

// define width
let w = 5; // change value as you need
// define height
let h = 8; // change value as you need

// call rectPerimeter with params i.e. width and height
console.log(rectPerimeter(w, h));

您还可以捕获返回值,然后将其显示到控制台:

function rectPerimeter(width, height) {
  return 2 * width + 2 * height;
}

// define width
let w = 5; // change value as you need
// define height
let h = 8; // change value as you need

// call rectPerimeter with params i.e. width and height
let perimeter = rectPerimeter(w, h);

// show perimeter to console
console.log(perimeter);

推荐阅读