簡體   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