Travis build: 884
This commit is contained in:
@ -690,7 +690,7 @@ median([0, 10, -2, 7]); // 3.5
|
||||
</code></pre><pre><code class="language-js">randomNumberInRange(2, 10); // 6.0211363285087005
|
||||
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="round">round</h3></div><div class="section double-padded"><p>Rounds a number to a specified amount of digits.</p><p>Use <code>Math.round()</code> and template literals to round the number to the specified number of digits. Omit the second argument, <code>decimals</code> to round to an integer.</p><pre><code class="language-js">const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
|
||||
</code></pre><pre><code class="language-js">round(1.005, 2); // 1.01
|
||||
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="solverpn">solveRPN</h3></div><div class="section double-padded"><p>Solves the given mathematical expression in <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">reverse polish notation</a>. Throws appropriate errors if there are unrecognized symbols or the expression is wrong.</p><p>Use a dictionary, <code>OPERATORS</code> to specify each operator's matching mathematical operation. Use <code>String.replace()</code> with a regular expression to replace <code>^</code> with <code>**</code>, <code>String.split()</code> to tokenize the string and <code>Array.filter()</code> to remove empty tokens. Use <code>Array.forEach()</code> to parse each <code>symbol</code>, evaluate it as a numeric value or operator and solve the mathematical expression. Numeric values are converted to floating point numbers and pushed to a <code>stack</code>, while operators are evaluated using the <code>OPERATORS</code> dictionary and pop elements from the <code>stack</code> to apply operations.</p><pre><code class="language-js">const solveRPN = rpn => {
|
||||
</code></pre></div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="solverpn">solveRPN</h3></div><div class="section double-padded"><p>Solves the given mathematical expression in <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">reverse polish notation</a>. Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- <code>+</code>,<code>-</code>,<code>*</code>,<code>/</code>,<code>^</code>,<code>**</code> (<code>^</code>&<code>**</code> are the exponential symbols and are same). This snippet does not supports any unary operators.</p><p>Use a dictionary, <code>OPERATORS</code> to specify each operator's matching mathematical operation. Use <code>String.replace()</code> with a regular expression to replace <code>^</code> with <code>**</code>, <code>String.split()</code> to tokenize the string and <code>Array.filter()</code> to remove empty tokens. Use <code>Array.forEach()</code> to parse each <code>symbol</code>, evaluate it as a numeric value or operator and solve the mathematical expression. Numeric values are converted to floating point numbers and pushed to a <code>stack</code>, while operators are evaluated using the <code>OPERATORS</code> dictionary and pop elements from the <code>stack</code> to apply operations.</p><pre><code class="language-js">const solveRPN = rpn => {
|
||||
const OPERATORS = {
|
||||
'*': (a, b) => a * b,
|
||||
'+': (a, b) => a + b,
|
||||
|
||||
Reference in New Issue
Block a user