Solved

I want to chop the last character of array elements in JavaScript...

Posted on 2008-10-29
6
294 Views
Last Modified: 2012-08-14
Hi there;

I want to chop the last character of array elements in JavaScript...

Apparently, i am doing something wrong...What am i doing stg wrong and how to fix it without changing the algorithm drastically?

Best regards...
<html>

<head>

<script>

function chop()

{

for (var i in this)

{

if (typeof this[i] == "string"){

this[i] = this[i].substring(0, this[i].length-1)

document.write(this[i]);   

}

}

}

Array.prototype.chop = chop
 

var line = new Array("red", "green", "blue")
 

</script>

</head>

<body>

</body>

</html>

Open in new window

0
Comment
Question by:jazzIIIlove
  • 3
  • 2
6 Comments
 
LVL 16

Assisted Solution

by:Lolly-Ink
Lolly-Ink earned 50 total points
ID: 22836924
You just need to call the chop function to see that it works already.
<html>

<script>

   function chop()

   {

      for (var i in this)

      {

         if (typeof this[i] == "string")

         {

            this[i] = this[i].substring(0, this[i].length-1);

            document.write(this[i]);   

         }

      }

   }

   Array.prototype.chop = chop

    

   var line = new Array("red", "green", "blue")

   line.chop();

</script>

</html>

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 450 total points
ID: 22836999
I suggest you do NOT use document.write within the function. Once the browser has finished loading you will not be able to call your chop() method. If leave the document.write in place and call chop() after the document has loaded you will "erase" everything that was previously on the window. Try:
<html>
<head>
<script>
function chop()
{
	for (var i in this)
	{
		if (typeof this[i] == "string"){
			this[i] = this[i].substring(0, this[i].length-1) 
		}
	}
}
Array.prototype.chop = chop
 
var line = new Array("red", "green", "blue") 

</script>
</head>
<body>
<script>
line.chop();
document.write( line.join("; ") )
document.write("<hr>");
document.write( line.join("; ") )
</script>
</body>
</html>

Open in new window

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 22843152
Lolly-Ink:
nice point...I forgot the call:)

hielo:
<<I suggest you do NOT use document.write within the function.

Is this a generalization, i mean using document.writeln() within the function is risky...or just for this case?

<<Once the browser has finished loading you will not be able to call your chop() method. If leave the <<document.write in place and call chop() after the document has loaded you will "erase" everything <<that was previously on the window

I am really confused about the point once the browser finished loading...I mean for example in your code, above  the browser has finished loading after </html> tag...right? or at which point?

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 82

Accepted Solution

by:
hielo earned 450 total points
ID: 22843237
>> i mean using document.writeln() within the function is risky
What I mean is that you can only use document.write on page while it is loading. After it has loaded, then you will "destroy" the previous page and then document.write will write to the new one. Consider this:
test1.html

<html>

<body>

<h1>Test</h1>

<script>

//this will get printed as the page is loaded

document.write('<p>Hello</p>');

</script>

</body>

</html>
 

test2.html

<html>

<body>

<h1>Test</h1>

<script>

//this will get printed as the page is loaded

function greet()

{

document.write('<p>Hello</p>');

}

/* "schedule" the greet function for execution after the page has finished loading. Where is the "Test" Heading?*/

window.onload=greet;

</script>

</body>

</html>

Open in new window

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 22843681
hielo!

I love your explanation...

Best regards...
0
 
LVL 12

Author Closing Comment

by:jazzIIIlove
ID: 31511483
not my code but the last message is hilarious...hielo!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now