跳转到内容

JavaScript/函数/练习

来自维基教科书,开放的书籍,开放的世界

主题:函数



1. 定义一个带一个参数的函数。该函数返回一个字符串,以“给定的参数是:”开头,以“. 好吗?”结尾。在两个字符串之间插入参数。

点击查看解决方案
"use strict";

// declare the function
function decorate(str) {
  const ret = "The given argument is: " + str + ". Ok?";
  return ret;
}

// call the function
let tmp = decorate("Francis");
alert(tmp);

tmp = decorate("Drake");
alert(tmp);



2. 扩展上一个函数以处理不同的数据类型,例如数字、对象等。在返回的字符串中显示数据类型。

点击查看解决方案
"use strict";

// declare the function
function decorate(param) {

  let dataType = "";
  let paramAsString = "";

  switch (typeof param) {
    case "object":
      dataType = "object";
      paramAsString = JSON.stringify(param);
      break;
    case "string":
      dataType = "string";
      paramAsString = param;
      break;
    // next case ...
    default:
      dataType = "???";
      paramAsString = param;
  }
  const ret = "The given argument is: " + paramAsString + " It's data type is: " + dataType + ". Ok?";
  return ret;
}

// call the function
let tmp = decorate("Francis");
alert(tmp);

tmp = decorate(42);  // a number
alert(tmp);

tmp = decorate({temperature: "25° C"}); // an object
alert(tmp);



3. 定义一个接受两个参数的函数。该函数返回两个参数的平均值。

点击查看解决方案
"use strict";

// declare the function
function average(num1, num2) {
  return (num1 + num2) / 2;
}

// call the function
alert(average(2, 4));

alert(average(2.4, 2.6));



4. 使用箭头函数定义第一个练习的函数。

点击查看解决方案
"use strict";

// declare the function and reverence it by a variable
const decorate = (str) => "The given argument is: " + str + ". Ok?";

// call the function
let tmp = decorate("Francis");
alert(tmp);


// or, even more compact:
tmp = (str => "The given argument is: " + str + ". Ok?")("Drake");
alert(tmp);



5. 定义一个函数,该函数接受一个数字数组作为参数。该函数返回数组的长度、最小元素、最大元素和所有元素的平均值。

由于函数只能返回单个元素,因此所有计算的值必须打包到一个数组(或对象)中。该数组是返回值。

点击查看解决方案
"use strict";

function checkArray(arr) {

  // local variables
  let min = arr[0];
  let max = arr[0];
  let sum = 0;

  // a 'forEach' construct with an anonymous function
  arr.forEach((elem) => {
    if (elem < min) { min = elem };
    if (elem > max) { max = elem };
    sum = sum + elem;
  });

  // the length and average can be computed here
  // 'pack' everything in an array:  []
  return [arr.length, min, max, sum / arr.length];
}

// the return value is an array
let ret = checkArray([2, 4, 6, 8]);
alert(ret);

// or:
let [length, min, max, avg] = checkArray([2, 4, 6, 8, 10]);
alert ("Length: " + length + ", Min: " + min + ", Max: " + max + ", Average: " + avg);



6. 定义一个描述汽车的函数。它接受四个参数:生产商、型号、马力、颜色。前两个是必需的;后两个是可选的。为后两个参数定义默认值,例如“未知”。

点击查看解决方案
"use strict";

// supply default values if they are not given
function showCar(producer, model, horsepower = "unknown", color = "unknown") {

  let ret = "";
  if (!producer || !model) {
    return "Producer and model must be specified";
  }
  ret  = "My car is produced by " + producer;
  ret +=  ". The model is: " + model;
  ret +=  ". The horsepower is: " + horsepower;
  ret +=  ". The color is: " + color;

  return ret;
}

alert(showCar("Ford", "Mustang", 300, "blue"));
alert(showCar("Ford", "Mustang GT", 350));



1 结果是什么?

"use strict";

alert(func("Hello"));

function func(param) {
  return param + " world";
} //

你好,世界
运行时错误
以上都不是

2 结果是什么?

"use strict";

function func(param) {
  param = "ppp";
  return param;
} //

let x = "xxx";

alert(x);
alert(func(x));
alert(x);

xxx, xxx, xxx
xxx, ppp, ppp
xxx, ppp, xxx
以上都不是

华夏公益教科书