猿问

我相实现当我输出5的时候才退出整个计算器,输入非数值结束数值输入,必须用到函数重载。

<!DOCTYPE html>

<html lang="en">


<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>【函数重载】编写一个方法,能接收若干数值 和 一个 运算方法,获取若干数值的 运算结果。</title>

<script>

function add(arr) {

var sum = 0;

for (var i = 0; i < arguments.length; i++) {

for (var x = 0; x < arr.length; x++) {

sum += arguments[i][x];

}

}

return sum;

}


function multiple(arr) {

var sum = 1;

for (var i = 0; i < arguments.length; i++) {

for (var x = 0; x < arr.length; x++) {

sum *= arguments[i][x];

}

}

return sum;

}


function division(arr) {

var sum = 1;

for (var i = 0; i < arguments.length; i++) {

for (var x = 0; x < arr.length; x++) {

sum /= arguments[i][x];

}

}

return sum;

}


function sole(arr) {

var sum = 0;

for (var i = 0; i < arguments.length; i++) {

for (var x = 0; x < arr.length; x++) {

sum -= arguments[i][x];

}

}

return sum;

}



function start(selectOperate, inputNum, add, multiple, division, sole, main) {

var isOk = true;

main(selectOperate, inputNum, add, multiple, division, sole);

}


function main(selectOperate, inputNum, add, multiple, division, sole) {

var a = selectOperate();

var sum = 0;

var doOperateNum = 0;

switch (a) {

case 1:

doOperateNum = inputNum();

sum = add(doOperateNum);

console.log(sum);

break;

case 2:

doOperateNum = inputNum();

sum = sole(doOperateNum);

console.log(sum);

break;

case 3:

doOperateNum = inputNum();

sum = multiple(doOperateNum);

console.log(sum);

break;

case 4:

doOperateNum = inputNum();

sum = division(doOperateNum);

console.log(sum);

default:

break;

}

}


function selectOperate() {

var selecNum = parseInt(prompt('请输入您要进行的操作。1.加法,2.减法,3.乘法,4.除法.5.退出'));

return selecNum;

}

function inputNum() {

var isOk = true;

var arr = [];

var a = 0;

do {

var inputNum = parseFloat(prompt('请输入您要进行计算的值'));

arr[a] = (inputNum);

a++;

if (isNaN(inputNum) || inputNum == null || inputNum == '') {

isOk = false;

arr.pop();

}

} while (isOk);

return arr;

}

start(selectOperate, inputNum, add, multiple, division, sole, main);

</script>

</head>


<body>

</body>

</html>


伪装的很酷
浏览 657回答 3
3回答

慕设计2395807

RNHNUAQQQILXYHMILILSVLGCSJWMRUHASIHXTOOFVREKIPECWZDWJCMBRQAACZTRHDTMPGWWCSNKJOUJTXXQKJREKXCTJQQGFVLMPWEICYLRBAPJFVVOUNVFFHGFWGZHHQTGTMNGZZTDGDGUEAKQJDWTCWVPOYBKRXMMBYKQKTNNTSZIPLBLRRDNPJJEZVQJSZFEOBUAMCCVLYRATXQOKXNDGJJFWZYIIYLFUUEODNMJTJPYYTVOGWFDWWWCIHQMCWWXEARDQJSZLLJGZJCTGMFPFLHHEIWLOEGWFBXNKOKAWSVRHDAWTWLVFVEHWEBWTPFBEORTWSVRHQZVLHXACAVYBQVLAILNLBQGVZJXGJEWMBRNKHKGCFMPXNDTXAWSIMWYURNJMILSMOENOEGQSIRNEMWZOEHQMHKGPYNKNPXTPGPENWMBMBYHOCEHQZQGVYOLWNNMZSPIUXJLGJRUXHCSODMHXOKAKFVSHDZVYHQBETWEHCMVKAJYHXIJTWFVGCYAQRHDTCMPMHWYBYTDENKMCFOZWLUDRHEZPFOXMPYNQTCLHVLNQSWGPYGOCETJLOLBJMIBKTWFIKTWTVMPYHQBPYUXZBDTXMPFBKAIQGIFWZWGVLULUDGPXHQSIRVKHQUKZIYHWWZCSSSBVTJWSIRNQMPLUXTQLCFUYBDSVLROJGPRIYHXHXAJMVDUCEHXSNKGPFBYNKTSIKGJFOLHDZCLUDZPYOEUJTWLILACAVLUSBLHDGWNQRTOJFDSVLNDGITJLVLUQYNWFENCMPSIFPSBHKAQMWMOEZWBLBEURHDMCRHWSPROLVLBDAWLPLHRGWMIGDLTVSCFILTHJZCLHXBQZPQGDSIRAXMPYLOYHXAWNDTC

慕移动2103324

你展示的代码没有运行错误啊?你想问什么问题呢?是不是你想每次计算完成后都可以重新选择操作?那可以把main方法改成:var a = 0; do{     a = selectOperate();     var sum = 0;     ...     ... }while(a != 5);应该就可以了吧?
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答