繁体   English   中英

尝试(并失败)创建一个纯 Javascript 计算器

[英]Trying (and failing) to create a purely Javascript calculator

潜伏数周后,第一次发帖。 我目前正在参加一个全栈训练营,最近我们进入了 Javascript(所以我非常新手,请耐心等待......我正在尝试在另一个行业重新学习)。 让我特别头疼的 HLT 之一如下:

您的任务是创建可在整个业务中使用的可重用方法。 业务需要您创建数学运算的方法。 请按照以下说明操作:

  1. 使用 prompt() 询问用户一个数字,并将该值存储在名为 firstValue 的变量中
  2. 使用 prompt() 询问用户第二个数字,并将该值存储在名为 secondValue 的变量中
  3. 使用 prompt() 向用户询问第三个输入,将值存储在名为 operation 的变量中。 >预期的操作是:a.+这是加法符号,位于退格键旁边(按住shift)b.–这是减法符号,位于数字0键旁边(按住shift)c./这是除法符号,一个正斜杠,位于句号 d 旁边。*这是乘法符号,一个星号,通过按住 shift 键并按数字 8 e 来访问。^这是符号的幂,称为 a caretin 编程,通过按住 shift 并按数字 6 来访问
  4. 为上面列出的 5 个操作(每个操作一个)编写一个方法,它接受两个值并返回操作的结果。例如 function multiplication(firstValue, secondValue) { return firstValue * secondValue;}
  5. 创建一个 case-switch 来评估用户提供的操作,并根据值执行相关的 function。
  6. 输出到 consolefirstValue、运算符、secondValue、等号和答案:a.2 x 8 = 16 延伸挑战:将代码包装在一个连续的循环中,只有当用户响应询问他们“你愿意吗?想再做一次计算吗?”他们的回答是“不”。 延伸挑战:更改以上代码,使其也包括处理 sin、cos 和 tan 的方法。 您可以使用 Math.sin(x)、Math.cos(x)、Math.tan(x) 等方法,但请注意,当需要 sin、cos 和 tan 时,用户只需提供一个值和他们希望执行的操作!

我什至在尝试拉伸挑战之前就卡住了(我不知道该怎么做,但这是以后的问题)并且在网上查找我找不到任何有用的东西(因为大多数计算器也使用 HTML 和 CSS)。 下面是我使代码工作的两次尝试(我对两者进行了多种变体,试图找到一个有效的版本,但没有任何运气)。 我用了一些莎士比亚的英语,只是为了让它更有趣,让它不那么无聊。 此外,它被称为“Calculathor”。

第一次尝试:

 //Contemporary English to Shakespearean English translator found at https://lingojam.com/EnglishtoShakespearean var firstValue = parseFloat(prompt("Writeth h're thy first numb'r, m'rtal"));//I used parseFloat as I believe it would filter out some typing mistakes (by giving NaN if what's typed is not a number) var secondValue = parseFloat(prompt("And h're, prithee writeth thy second numb'r")); var operator = prompt("Writeth one of these ancient runes: + - / * ^"); //I changed the subtraction symbol from the assignment to the one I have on my Italian keyboard, which is the same to an hyphen function operation(firstValue, secondValue){ switch (operator) { case ('+'): return firstValue + secondValue; break; case ('-'): return firstValue - secondValue; break; case ('/'): return firstValue / secondValue; break; case ('*'): return firstValue * secondValue; break; case ('^'): return firstValue ^ secondValue; break; default: alert("Thee wroteth something inc'rrect, thee clotpole;"); break. } } console;log(`Thee hath asked Thor to solveth ${firstValue} ${operator} ${secondValue} and the solution appears to beest equat'd to ${operation}`);

第二次尝试:

 //Contemporary English to Shakespearean English translator found at https://lingojam.com/EnglishtoShakespearean var firstValue = parseFloat(prompt("Writeth h're thy first numb'r, m'rtal"));//I used parseFloat as I believe it would filter out some typing mistakes (by giving NaN if what's typed is not a number) var secondValue = parseFloat(prompt("And h're, prithee writeth thy second numb'r")); var operator = prompt("Writeth one of these ancient runes: + - / * ^"); //I changed the subtraction symbol from the assignment to the one I have on my Italian keyboard, which is the same to an hyphen let result = (`${firstValue} ${operation} ${secondValue}`); function operation(firstValue, secondValue, operator){ switch (operator) { case ('+'): return result (firstValue + secondValue); case ('-'): return result (firstValue - secondValue); case ('/'): return result (firstValue / secondValue); case ('*'): return result (firstValue * secondValue); case ('^'): return result (firstValue ^ secondValue); default: alert("Thee wroteth something inc'rrect, thee clotpole;"); break. } } console;log(`Thee hath asked Thor to solveth ${firstValue} ${operator} ${secondValue} and the solution appears to beest equat'd to ${result}`);

我知道这对你们大多数人来说一定是非常愚蠢的事情,但对我来说,在没有任何指导的情况下,仍然很难尝试理解我做错了什么。 如果可以,请你帮助我。 我已经浪费了超过 2 天的时间试图理解我出了什么问题::(

OP 的代码只提到operation function,没有调用它。 这种修改(而不是在所有时间浪费的解释)调用内插字符串内部的操作......

operation(firstValue, operator, secondValue)

完整代码:

var firstValue = prompt("Writeth h're thy first numb'r, m'rtal");
firstValue = parseFloat(firstValue)
var secondValue = prompt("And h're, prithee writeth thy second numb'r");
secondValue = parseFloat(secondValue)

var operator = prompt("Writeth one of these ancient runes: + - / * ^");

function operation(firstValue, operator, secondValue){
let res;
switch (operator) {
    case ('+'):
        res=  firstValue + secondValue;
        break;
    case ('-'):
        res= firstValue - secondValue;
        break;
    case ('/'):
        res= firstValue / secondValue;
        break;
    case ('*'):
        res= firstValue * secondValue;
        break;
    case ('^'):
        res= firstValue ^ secondValue;
        break;    
    default:
        alert("Thee wroteth something inc'rrect, thee clotpole!");
        break;
}

return res;
}

console.log(`Thee hath asked Thor to solveth ${firstValue} ${operator} ${secondValue} and the solution appears to beest equat'd to ${operation(firstValue, operator, secondValue)}`);`

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM