How can we add square and cube root function button in HTML calculator? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can we add square and cube root function button in HTML calculator?

17th Jun 2017, 8:30 AM
Rishita
Rishita - avatar
12 Answers
+ 4
In the function of square root { sqrt = number **(1/2) return sqrt } In the function of cube root { cubert = number **(1/3) return cubert } I hope this helps
17th Jun 2017, 2:22 PM
Siddharth Saraf
+ 3
Your source code is messy, and probably copied from at least same source than: https://code.sololearn.com/WSRPKQa7f70G/?ref=app https://www.sololearn.com/Discuss/470965/?ref=app https://www.sololearn.com/Profile/2336081/?ref=app You need to specify the original author in your "adaptation" (wich consist mostly to ask other sololearners to give you solution for adding 2 mathematical functions :P) Anyway, for implementing square and cube roots, you can add this button code: <input type="button" value="√" onClick="document.calculator.ans.value='Math.sqrt('+document.calculator.ans.value+')'"> <input type="button" value="∛" onClick="document.calculator.ans.value='Math.cbrt('+document.calculator.ans.value+')'">
17th Jun 2017, 12:57 PM
visph
visph - avatar
+ 3
@rishita wrote in the @M. Oly Mahmud probably ny mistake: << sorry,your suggestions are not working too.Did you try it in the calculator "invented"by you? I am here, in this forum,to learn,not to do copy and paste.I have done it in my way,so it may be messy.As I am learning as a beginner,I am bound to ask questions,to learn and improve,not to impress! >> I'm not telling you steal for impress, I just advise you to cite your source, at it seems obvious there's one ^^ Anyway, my suggestions perfectly works since it's right implemented :P https://code.sololearn.com/Wd6yQTls79ky/#html
17th Jun 2017, 1:40 PM
visph
visph - avatar
+ 3
And I'm not 'inventor of calculator' at all...
17th Jun 2017, 1:42 PM
visph
visph - avatar
+ 3
Notice that formulate my remark did not prevent me to give you answer to your questions... And rather than says that suggested codes doesn't works, post link to your attempt of integrate them so we can give you some advice about your probably mistakes. Else, we can almost provide you entirely working code as I done but not give you accurate "positive" advice :P
17th Jun 2017, 1:51 PM
visph
visph - avatar
+ 2
square root = <math><msqrt><mi>2</mi></msqrt></math> but easier, in JavaScript: Math.sqrt() and Math.cbrt()
17th Jun 2017, 8:41 AM
Maart
Maart - avatar
+ 2
Sorry! This code is not working!
17th Jun 2017, 8:49 AM
Rishita
Rishita - avatar
+ 2
As bonus, there's the code for a 'delete' button: <input type="button" value="Del" onclick="document.calculator.ans.value=document.calculator.ans.value.substring(0,document.calculator.ans.value.length-1)"> ... and in addition, a fix for your 'dot' button: <input type="button" value="." onClick="document.calculator.ans.value+='.'">
17th Jun 2017, 1:07 PM
visph
visph - avatar
+ 2
sorry Mr "inventer of Calculator"! your suggestions are not working too.Did you try it in the calculator "invented"by you? I am here, in this forum,to learn,not to do copy and paste.I have done it in my way,so it may be messy.As I am learning as a beginner,I am bound to ask questions,to learn and improve,not to impress!
17th Jun 2017, 1:38 PM
Rishita
Rishita - avatar
+ 2
stealing is irrelevant and I didn't consult any previous presentation before making my work.Anyway,I need positive advice only so that I can improve.
17th Jun 2017, 1:45 PM
Rishita
Rishita - avatar
+ 2
@Siddhart Saraf: Your solution doesn't work, as '**' operator doesn't exist in JS (it's at least a Python operator), and you should do: function square_root(num) { return Math.pow(num,1/2); } function cube_root(num) { return Math.pow(num,1/3); } alert(square_root(4)); alert(cube_root(27)); No need of this mathematical trick anyway in this case, as JS provide built-in Math.sqrt() and Math.cbrt() functions ^^
17th Jun 2017, 2:44 PM
visph
visph - avatar
+ 2
You can see that in this code -- https://code.sololearn.com/W2J8H89mUb07/?ref=app
2nd Jan 2019, 2:09 PM
Mrityunjay
Mrityunjay - avatar