简体   繁体   English

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

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

First post ever, after lurking for some weeks.潜伏数周后,第一次发帖。 I'm currently attending a full-stack bootcamp and recently we got into Javascript (so I'm extremely green, please be patient...I'm trying to reskill myself in another industry).我目前正在参加一个全栈训练营,最近我们进入了 Javascript(所以我非常新手,请耐心等待......我正在尝试在另一个行业重新学习)。 One of the HLTs that is giving me a particular headache is as follows:让我特别头疼的 HLT 之一如下:

You are tasked with creating re-usable methods that can be used throughout the business.您的任务是创建可在整个业务中使用的可重用方法。 The business needs you to create methods for mathematics operations.业务需要您创建数学运算的方法。 Follow the instructions below:请按照以下说明操作:

  1. Ask the user for a number with a prompt() and store the value in a variable called firstValue使用 prompt() 询问用户一个数字,并将该值存储在名为 firstValue 的变量中
  2. Ask the user for a second number with a prompt()and store that value in a variable called secondValue使用 prompt() 询问用户第二个数字,并将该值存储在名为 secondValue 的变量中
  3. Ask the user for a third input with prompt()storing the value in a variable called operation.使用 prompt() 向用户询问第三个输入,将值存储在名为 operation 的变量中。 >Expected operations are: a.+This is the addition symbol, located next to the backspace key(hold shift) b.–This is the subtraction symbol, located next to number 0key (hold shift) c./This is the division symbol, a forward slash, located next to the full stop key d.*This is the multiplication symbol, a star, accessed by holding shift and pressing number 8 e.^This is the to-the-power-of symbol, known as a caretin programming, accessed by holding shift and pressing the number 6 >预期的操作是:a.+这是加法符号,位于退格键旁边(按住shift)b.–这是减法符号,位于数字0键旁边(按住shift)c./这是除法符号,一个正斜杠,位于句号 d 旁边。*这是乘法符号,一个星号,通过按住 shift 键并按数字 8 e 来访问。^这是符号的幂,称为 a caretin 编程,通过按住 shift 并按数字 6 来访问
  4. Write a method for the 5 operations listed above (one for each operation) that takes in both valuesand returns the result of the operation.a.For examplefunction multiplication(firstValue, secondValue) { return firstValue * secondValue;}为上面列出的 5 个操作(每个操作一个)编写一个方法,它接受两个值并返回操作的结果。例如 function multiplication(firstValue, secondValue) { return firstValue * secondValue;}
  5. Create a case-switch for evaluating the operation the user supplied, and depending on the value, execute the relevant function.创建一个 case-switch 来评估用户提供的操作,并根据值执行相关的 function。
  6. Print out to consolefirstValue, operator, secondValue, an equal sign, and the answer: a.2 x 8 = 16 STRETCH CHALLENGE: Wrap the code in a continuous loop that only ends when the user responds to a prompt that asks them “would you like to do another calculation?”with their answer being “no”.输出到 consolefirstValue、运算符、secondValue、等号和答案:a.2 x 8 = 16 延伸挑战:将代码包装在一个连续的循环中,只有当用户响应询问他们“你愿意吗?想再做一次计算吗?”他们的回答是“不”。 STRETCH CHALLENGE: Change the above code to also include methods for processing sin, cos, and tan.延伸挑战:更改以上代码,使其也包括处理 sin、cos 和 tan 的方法。 You can use the methodsMath.sin(x), Math.cos(x), Math.tan(x)but be aware thatthe user only needs to supply a single value and the operation they wish to dowhen needing sin, cos, and tan!您可以使用 Math.sin(x)、Math.cos(x)、Math.tan(x) 等方法,但请注意,当需要 sin、cos 和 tan 时,用户只需提供一个值和他们希望执行的操作!

I'm stuck even before attempting the stretch challenges (which I have no clue on how to do, but that's a problem for later) and looking online I couldn't find anything helpful (since most calculators employ HTML and CSS as well).我什至在尝试拉伸挑战之前就卡住了(我不知道该怎么做,但这是以后的问题)并且在网上查找我找不到任何有用的东西(因为大多数计算器也使用 HTML 和 CSS)。 Here below my two attempts at making the code work (I made multiple variations of both, trying to find a version that worked, but without any luck).下面是我使代码工作的两次尝试(我对两者进行了多种变体,试图找到一个有效的版本,但没有任何运气)。 I used some Shakespearean English, just to spice it up and to make it less boring.我用了一些莎士比亚的英语,只是为了让它更有趣,让它不那么无聊。 Also, it's called "Calculathor".此外,它被称为“Calculathor”。

First attempt:第一次尝试:

 //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}`);

Second attempt:第二次尝试:

 //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}`);

I know this must be something very stupid for most of you, but for me it's still pretty hard to try and understand what I'm doing wrong, without any guidance.我知道这对你们大多数人来说一定是非常愚蠢的事情,但对我来说,在没有任何指导的情况下,仍然很难尝试理解我做错了什么。 Please help me, if you can.如果可以,请你帮助我。 I've wasted already more than 2 days trying to understand what I'm getting wrong: :(我已经浪费了超过 2 天的时间试图理解我出了什么问题::(

The OP's code only mentioned the operation function, failing to invoke it. OP 的代码只提到operation function,没有调用它。 This modification (and not-at-all-time-wasting explanation) invokes operation inside the interpolated string...这种修改(而不是在所有时间浪费的解释)调用内插字符串内部的操作......

operation(firstValue, operator, secondValue)

The complete code:完整代码:

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