简体   繁体   中英

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). One of the HLTs that is giving me a particular headache is as follows:

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!

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). 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".

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: :(

The OP's code only mentioned the operation function, failing to invoke it. 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)}`);`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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