Skip to content

Instantly share code, notes, and snippets.

@olvrb
Created October 25, 2018 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olvrb/78a9928c1aeaf2c5f0d5e8b424337890 to your computer and use it in GitHub Desktop.
Save olvrb/78a9928c1aeaf2c5f0d5e8b424337890 to your computer and use it in GitHub Desktop.
get deriv of function
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