Jump to content
Bitcoinani

Help me with JavaScript!!

Recommended Posts

 Hello my pd frnds...

I'm learning JavaScript and i've tried solving this geometric progression without success. The first and last terms are given which are a and b respectively according to the image attached. d is the common ratio and n which is the number of terms is not given. My solution prints way more than expected, my answer shouldn't be anywhere over the last term. This example should terminate at 27 . thereby adding only 3 items to the array.



Solution attached as image


http://postimg.org/image/1s7bg490zf/67f68eda/

Share this post


Link to post
Share on other sites
3 minutes ago, iwant1btc said:

I would help u with heart, but i don't know so much nani  :P 

Haha Its k... iwant1btc... i know that if you know you would have surely help me :)

Share this post


Link to post
Share on other sites
9 minutes ago, iwant1btc said:

I think u can get answer from google. Google can help your all question about JavaScript

i searched on google and didnt find so i asked here

6 minutes ago, Noahbreezy said:

If he's not finding the answer himself he learned nothing.

TIP: Cut the code in pieces, write down what the code does in every line. This can help you.

okay i will try to execute each piece perfectly 

Share this post


Link to post
Share on other sites
1 minute ago, Serlite said:

@Bitcoinani Any luck? If you'd like some clues, I think I can nudge you in the right direction. Let me know if you've gotten any closer to a working solution.

No serlite, I was just waiting for you to get your eyes on my topic... i think now my problem will be solved :)

Share this post


Link to post
Share on other sites
1 minute ago, Bitcoinani said:

No serlite, I was just waiting for you to get your eyes on my topic... i think now my problem will be solved :)

Hahaha, I'm not the only one who knows JavaScript here, you know. =P So, I'll start you off with some leading questions. Look through your code and see if you can find the problem I'm pointing at with them:

  1. Right now, you have a for loop nested inside a do-while loop. Your while condition is what checks whether you've reached the required term - when is it checked right now? How could you rearrange your code so it could be checked at a different time (possibly earlier)? Do you really need two loops?

Share this post


Link to post
Share on other sites
3 minutes ago, Serlite said:

Hahaha, I'm not the only one who knows JavaScript here, you know. =P So, I'll start you off with some leading questions. Look through your code and see if you can find the problem I'm pointing at with them:

  1. Right now, you have a for loop nested inside a do-while loop. Your while condition is what checks whether you've reached the required term - when is it checked right now? How could you rearrange your code so it could be checked at a different time (possibly earlier)? Do you really need two loops?

Oh i understood now... so what if i do it with a single while loop?

Share this post


Link to post
Share on other sites
1 minute ago, Serlite said:

I think you'd have a much easier time arranging your logic - that's the way I solved the problem!

oh okay sir, wait i m doing it.. :)

OMG serlite sir I solved it thank you so muchhh...

 

function gp(a,b) { 

       var arr = [];

       var d = 3;

       while (a<b){

       arr.push(a); a = a*d;

       }

   console.log(arr);

}

 

Thank you your presence solved my problem :)

Share this post


Link to post
Share on other sites
42 minutes ago, Bitcoinani said:

oh okay sir, wait i m doing it.. :)

OMG serlite sir I solved it thank you so muchhh...

 

function gp(a,b) { 

       var arr = [];

       var d = 3;

       while (a<b){

       arr.push(a); a = a*d;

       }

   console.log(arr);

}

 

Thank you your presence solved my problem :)

Well done! Glad you were able to get the solution in the end.

Good luck on your continued learning of JavaScript!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×