## DEV Community is a community of 871,998 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Cesar Del rio

Posted on • Updated on

# #33 -Derivatives of type x^n - CodeWars Kata (6 kyu)

## Instructions

You are provided with a function of the form f(x) = axⁿ, that consists of a single term only and 'a' and 'n' are integers, e.g f(x) = 3x², f(x) = 5 etc.

Your task is to create a function that takes f(x) as the argument and returns the result of differentiating the function, that is, the derivative.

If f(x)=axn f(x) = ax^nf(x)=ax
n
, then f′(x)=naxn−1 f^{\prime}(x) = nax^{n-1}f

(x)=nax
n−1

Input is a string, for example "5x^4". The function f(x) consists of a single term only. Variable is denoted by x.
Output should be a string, for example "20x^3".

Examples

"3x^2" => "6x"
"-5x^3" => "-15x^2"
"6x^-2" => "-12x^-3"
"5x" => "5"
"-x" => "-1"
"42" => "0"

## My solution:

function differentiate(f) {
if(!f.includes('x')) return "0"
if(f == 'x') return "1"
if(f == '-x') return "-1"
if(!f.includes('^')) return f.replace('x', '')

f=f.split('^')
if(f[0] == 'x' || f[0] == '-x') f[0]=f[0].replace('x','1')
f[0] = f[0].replace('x','')

let exponent = +f[1]
let base = +f[0]
let newExponent = +f[1] - 1
let newBase = exponent*base

if(newExponent == 1) return newBase + 'x'
if(newBase == -1) return '-x^' + newExponent
if(newBase == 1) return 'x^' + newExponent
return newBase + 'x^' + newExponent

}


## Explanation

First I used some conditionals for especific inputs, if the function doesn't includes an "x" it is just a number like "42", so the result should be 0

if(!f.includes('x')) return "0"

If it is just "x" it'll return 1, and if it is "-x" it'll return -1

if(f == '-x') return "-1"

And if it doesn't includes a "^" it'll return the function but without the x, so if I have "6x" it'll return "6"

if(!f.includes('^')) return f.replace('x', '')

After that I splitted in the "^" and I used a conditional to see that if in the first element that is the base before the "^" I have only a "x" or a "-x" those are equal to 1 or -1, so I replaced "x" for 1 and "-x" for -1

f=f.split('^')
if(f[0] == 'x' || f[0] == '-x') f[0]=f[0].replace('x','1')

For example: x^2 --> [1, 2]

If not, I'll just eliminate the "x" replacing it to an empty string

f[0] = f[0].replace('x','')

After that using the array that I splitted, I declarated the exponent and the base, after that I created the newExponent resting 1 to the exponent and the newBase multiplying the base for the exponent.

let exponent = +f[1]
let base = +f[0]
let newExponent = +f[1] - 1
let newBase = exponent*base

Then I used some conditionals to return the last results, if the newExponent is 1, it should return the newBase and x, because if it is elevated to 1 it remains the same, if the newBase is equal to -1, it'll return "-x" and the newExponent, and if it is 1 it'll return "x" and the newExponent, and if any of this contions is true it'll return the newBase + 'x^' + newExponent.

if(newExponent == 1) return newBase + 'x'
if(newBase == -1) return '-x^' + newExponent
if(newBase == 1) return 'x^' + newExponent
return newBase + 'x^' + newExponent