Jump to content

How to Prepend Zeros to a Numerical Value


iwato

Recommended Posts

Dsonesuk:

this.parentElem.appendChild(this.newElem);

is returned as

this.appendChild(this.newElem);

and

appendChild(this.newElem)

is declared to be "not a function".

Roddy

Link to comment
Share on other sites

I don't understand?  how can coding change and return? As completely different from the code set out in script?

What browser are you using? I'll see if there's  some compatibility issue.

I tested this in Firefox and other main browsers and it worked fine, here's the whole code, just in case you are cross referencing the wrong object and its properties.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0">
        <title>Document Title</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script>

        </script>
        <style>

        </style>
    </head>
    <body>
        <div id="specificID">Sorry nothing to see here! </div>
        <script>
            Array.prototype.testArray4 = function(idref = "test_array4", parentElemID = document.body) {

                this.idref = idref;
                this.newElem = document.createElement("div");
                this.newElem.id = this.idref;
                this.parentElem = parentElemID;
                this.parentElem.appendChild(this.newElem);
                for (n = 0; n < this.length; n++) {

                    $('#' + this.newElem.id).append('<p>' + this[n] + '</p>');
                }
                $('#' + this.newElem.id).append('<hr>');
            }

            arrayObject4 = ["a", "b", "c", "d", "e", "f", "g", "h"];

            var x = document.getElementById("specificID");
            arrayObject4.testArray4(undefined, x);

        </script>
    </body>
</html>

 

Link to comment
Share on other sites

but it is specially wrapped Javascript

What does that mean, in a technical sense?  Does the browser treat jQuery any differently than it treats literally any other Javascript code?  Do you think that the Javascript engine inside the browser has a special section for handling jQuery code, or do you think it treats all Javascript code the same?  What does "specially wrapped" mean in a technical sense, what is the technical distinction which separates jQuery from any other code written in Javascript?

Yes, I have used document .write() many times in my own notes, because I tire quickly of the alert( ) function.

If you're just trying to see how everything works it's far more common to write to the console.  You can inspect the objects you send there, it will tell you which line number it's coming from, etc.  If you're trying to modify the page then you still need to tell the browser which element(s) you're trying to modify.  jQuery doesn't change that.

Link to comment
Share on other sites

Dsonesuk:  Please read the phrase "is returned as'  to mean "is returned by the FireFox Web Console as".

So, I have experimented with your code in a fresh document with some minor alterations of my own to test its robustness in a new CSS environment.  It performs as expected in one way, but not in another. 

Can you explain why the following code works, but what follows does not.  Are they not both doing the same thing?

Dsonesuk's Expression

<script>
    arrayObject4 = ["a", "b", "c", "d", "e", "f", "g", "h"];
    var x = document.getElementById("specificID");
    arrayObject4.testArray4(undefined, x);
</script>

Roddy's Expression

<script>
    arrayObject4 = ["a", "b", "c", "d", "e", "f", "g", "h"];
    var x = $('#specificID');
    arrayObject4.testArray4(undefined, x);
</script>

Roddy

Edited by iwato
Link to comment
Share on other sites

The reason why that matters is because when jQuery wraps an element it does not have an appendChild method like a regular DOM element object does.  jQuery objects use append instead of appendChild.  So this line fails:

this.parentElem.appendChild(this.newElem);
Link to comment
Share on other sites

OK.  I have now tried my first Number function.  Unfortunately,  I did not succeed.  Please find the original workable code followed by the unworkable newly introduced code.

WORKABLE CODE:

$('#so_average_time_spent').html(visitsSummary[0].avg_time_on_site);

The FUNCTION:

Number.prototype.annotate = function(String(unit)) {
	var s = String(this);
	s = s + " " + unit;
	return s;
}

The APPLICATION:

var avgSiteTime = visitsSummary[0].avg_time_on_site;
$('#so_average_time_spent').html(avgSiteTime.annotate('sec'));

There are no error messages.

Roddy

Link to comment
Share on other sites

Rather than asking me to expose my already very vulnerable operating environment, please provide me with the syntax error so that I might fix the problem.

Roddy

Link to comment
Share on other sites

Asking you to expose your vulnerable development environment?  I don't know what that means, but you really need a reliable way to find error messages.  Posting code online and waiting for someone to inspect it isn't efficient if all you need to do is determine if a piece of code has a syntax error.  You should really set up whatever you need so that you can see error messages, and maybe make your development environment a little less vulnerable.  I don't think there's any piece of information I could give you about my envrionment which would allow you to attack me, so I'm not sure why this information is secret.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...