跳轉到內容

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
以上都不是

華夏公益教科書