尝试(但失败了)创建一个纯粹的 Javascript 计算器

Trying (and failing) to create a purely Javascript calculator

有史以来第一个 post,在潜伏了几个星期之后。我目前正在参加一个全栈训练营,最近我们进入了 Javascript(所以我非常新手,请耐心等待......我正在尝试在另一个行业重新学习技能)。让我特别头疼的 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
  2. Ask the user for a second number with a prompt()and store that value in a variable called secondValue
  3. Ask the user for a third input with prompt()storing the value in a variable called 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
  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. Create a case-switch for evaluating the operation the user supplied, and depending on the value, execute the relevant 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”. STRETCH CHALLENGE: Change the above code to also include methods for processing sin, cos, and 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!

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

第一次尝试:

//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 函数,未能调用 它。此修改(和 not-at-all-time-wasting 解释)调用内插字符串内部的操作...

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