Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 591

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T12:18:09+00:00 2024-11-25T12:18:09+00:00

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

  • 62k

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  } 
Enter fullscreen mode Exit fullscreen mode


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


What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

100daysofcodejavascripttutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.