Created
October 25, 2018 13:48
-
-
Save olvrb/78a9928c1aeaf2c5f0d5e8b424337890 to your computer and use it in GitHub Desktop.
get deriv of function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require("readline-sync"); | |
let equation = readline.question("equation > "); | |
equation = equation.split(/ /g); | |
let eq = ""; | |
for (const term of equation) { | |
if (term.match(/(\+|-)/g)) { | |
eq += term; | |
continue; | |
} | |
let n = term.substr(term.indexOf("^") + 1, term.length); | |
let k = term.substr(0, term.indexOf("x")) || 1; | |
console.log(deriv(k, n)); | |
eq += deriv(k, n); | |
} | |
console.log(eq); | |
/* let k = readline.question("k > ", { hideEchoBack: false }); | |
let n = readline.question("n > ", { hideEchoBack: false }); | |
console.log(`${k * n}x${n - 1 === 0 ? "^" + n - 1 : ""}`); | |
*/ | |
function deriv(k, n) { | |
return `${k * n}x${n - 1 !== 0 ? "^" + (n - 1).toString() : ""}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment