Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Algorithmic Thinking

Guys , today i'm trying to implement Great Common Divisor with JavaScript. But seriously, even if my math level is really good, it is really hard for me to think in programming. What is your advice in order to solve math problems or any problems by thinking as programmer.

All in all, I found 3 approach to solve my problem , but it really took time. Is that normal?

function greatCommonDivisor(a,b){
    if (b===0){
        return a;
    }
    return greatCommonDivisor(a,a%b);
}

function greatCommonDivisor2(a,b){
    if (a ==0){
        return(b);
    }
    while(b > 0){
        if (a > b) {
            a = a - b;
        }else{
            b = b - a;
        }
    }
    return(a);
}


function greatCommonDivisor3(a,b){
    while(a > 0 && b > 0){
        if (a > b) {
            a = a % b;
        }else{
            b = b % a;
        }
    }
    return(a+b);
}
        if (a > b) {
            a = a % b;
        }else{
            b = b % a;
        }
    }
    return(a+b);
}

I fixed your post so the code displays properly. Make sure there is a line break before your opening and closing back ticks.

thanks Colin Marshall :)

2 Answers

As with many skills it takes practice. There are many sources/books on learning algorithms and that way of thinking. You may be interested in practicing with Codewars or Hackerrank. I've been seeing several coding bootcamps have students prepare with hacker rank questions. They also have a good diversity of the type of questions you may want to solve.

Hi Sage Elliott ! I'm already user in the hackerrank , for example in this problem , i can easily solve on the paper by using math. But when it comes to code, its really hard me to convert it as code. That's my problem. I dont know what to do.

When I first started concocting algorithms through code, this was a problem for me too. If you were able to look through the history of my questions/comments on Treehouse, you'd see quite a few dealing with just this. Sage is right - you have to just practice. Touch code as much as you can and try to think of an approach to a problem before coding first. That'll help you design solutions a lot better.

My only real advice I can give is to keep learning. As you are able to think more naturally in the context of a programming language it will get much easier to visualize the problem in code :)