Skip to content
Advertisement

Representation of an array as a polynomial

What this method should do is take an array of doubles and convert them into a polynomial, for example, if the array is given was [2.0, 3.0, -2.0] the method’s output would be 2.0x^2 + 3.0x^1 - 2.0. What I have done is created two loops however, when I do this both outputs are separated as expected. My output looks like this 2.0 1.0 2.0 1.0 x^3 x^2 x^1, if there is a way to have the exponents printed after the coefficient that would solve this issue.

JavaScript

Advertisement

Answer

You can build the resultString using one for-loop. Initialize a counter variable which you count backwards which is representing your ^3 part. This variable starts at your array.length-1 end runs down to zero in the iterations this is how you can create

JavaScript

It runs basically backwards so you should decrement it in each iteration

JavaScript

inside your one for-loop do

JavaScript

Here is a working solution

JavaScript

Produces the output

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement