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

Liam Maclachlan
Liam Maclachlan
22,805 Points

jQuery variable issue in a shared function

I have a function called 'stringToFloat' which is not quite working for me.

All the code is shared except 1 line that I need to find a clean way to alternate when necessary.

function valueChange() {
    // repeated code starts
    var float= /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
    var string = $("#project-change").val();
    var num = parseFloat(string);
    var textPercent = num.toFixed(0)
    // repeated code ends
}

function stageCheck() {
    $('.project-completion').each(function() {
        // repeated code starts
        var float= /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
        var string = $(this).find('h3').text();
        var num = parseFloat(string);
        var textPercent = num.toFixed(0)
        // repeated code ends

As you can see, it's the *var string = * line that I need to alternate between.

Anyone got any clean coding ideas for this? I don't really want to use an if statement with a boolean variable that is not needed but I think that this may be the only way.

I imagine it will need to look something like this. (Code is not working like this but I guess you get the gist :) )

function stringToFloat() {
    var float= /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;

    // pseudo code starts
    if (project-change is what you need) {
        var string = $("#project-change").val();
    }
    if (you want 'this' then run this) {
        var string = $(this).find('h3').text();
    }
    //pseudo code ends

    var num = parseFloat(string);
    var textPercent = num.toFixed(0)

}

Any ideas? My brain is fried :(

1 Answer

Hugo Paz
Hugo Paz
15,622 Points

Hi Liam,

If all code is shared except that line, why not create a function with the common code, pass it the string and return the result?

Something like this:

function valueChange() {

    var string = $("#project-change").val();
     var result = myFunction(string);
}

function stageCheck() {
    $('.project-completion').each(function() {

        var string = $(this).find('h3').text();
        var result = myFunction(string);

}

function myFunction(string){
        var float= /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
        var num = parseFloat(string);
        return num.toFixed(0); 
}
Liam Maclachlan
Liam Maclachlan
22,805 Points

Yes! That is brilliant.

I was several hours deep in to coding yesterday which went off on some serious tangents.

For some reason, I was adamant I could not move the string line... All of the things going through my mind involved having that string variable in the second line... Lesson learned. take a break every so often :)

Thanks man. I haven't tested it yet but that looks like it will definitely work.