I'm having trouble with an array of data created using the 'getElementsByTagName' call. I'm trying to remove duplicates from the Array. code follows: - // creates a list of all keywords and the removes any dublicates and printes the results
-
getKeywords : function() {
-
-
var keywords = articleHolder.getElementsByTagName('span');
-
var savedKeywords = [];
-
for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
-
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
-
}
-
-
if (allKeywords > 0) {
-
var para = document.createElement('p');
-
var strong = document.createElement('strong');
-
para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
-
-
savedKeywords.sort();
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
-
savedKeywords.splice(n,1);
-
-
}
-
else {
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
}
-
}
-
-
-
}
-
-
articleHolder.insertBefore(para, document.getElementById('copyright'));
-
}
13 1968
What kind of errors do you get?
Do you have some code for articleHolder to provide too?
I get no errors, but the code is not removing the duplicates from the Array. Intire code follows. - var pageOptions = {
-
-
articleHolder : null,
-
-
// get the JS to starting doing its job
-
init : function() {
-
if (document.getElementById) {
-
articleHolder = document.getElementById('content'), // the div holding the article;
-
pageOptions.addButtons();
-
pageOptions.getKeywords();
-
}
-
},
-
-
// create the h3, form, p, input, and input text; append the latter three and add function to each of the buttons
-
addButtons : function() {
-
// create title for the section
-
var formTitle = document.createElement('h3');
-
formTitle.appendChild(document.createTextNode('Customize Article Display'));
-
-
var buttonsForm = document.createElement('form');
-
buttonsForm.setAttribute('method','post');
-
buttonsForm.setAttribute('action','');
-
buttonsForm.setAttribute('id','buttons');
-
-
-
-
// variable for each button for asigning value
-
var actionButtons = [];
-
-
// create the p elements with input elements appened to each p element
-
for (var i = 0; i < 4; i++) {
-
var para = document.createElement('p');
-
actionButtons[i] = document.createElement('input');
-
actionButtons[i].setAttribute('type','button');
-
-
buttonsForm.appendChild(para).appendChild(actionButtons[i]);
-
}
-
-
// set deafult value for each button
-
actionButtons[0].setAttribute('value','Increase Line Spacing');
-
actionButtons[1].setAttribute('value','Highlight Keywords');
-
actionButtons[2].setAttribute('value','Highlight Text on Hover');
-
actionButtons[3].setAttribute('value','Hide Quotations');
-
-
// get Element ID for the container and the h3 elements within the container
-
var container = document.getElementById('rightcol');
-
var elementH3 = container.getElementsByTagName('h3');
-
-
-
-
for (var n = 0; n < elementH3.length; n++) {
-
-
// insert the form after the firt non javScript h3 element
-
container.insertBefore(buttonsForm, elementH3[0]);
-
}
-
-
// inserted the h3 title tag at the top of the container
-
container.insertBefore(formTitle, container.firstChild);
-
-
-
// add functions to each button
-
actionButtons[0].onclick = pageOptions.alterLineSpacing;
-
actionButtons[1].onclick = pageOptions.highlightKeywords;
-
actionButtons[2].onclick = pageOptions.highlightSelections;
-
actionButtons[3].onclick = pageOptions.alterQuoteDisplay;
-
-
},
-
-
// modify the line spacing based on the input button value
-
alterLineSpacing : function() {
-
-
if (this.getAttribute('value') == 'Increase Line Spacing') {
-
articleHolder.className = 'morelineheight';
-
this.setAttribute('value','Decrease Line Spacing');
-
}
-
else {
-
articleHolder.className = '';
-
this.setAttribute('value','Increase Line Spacing');
-
}
-
},
-
-
// modify keyword highlight based on the input button value
-
highlightKeywords : function() {
-
-
var theKeywords = articleHolder.getElementsByTagName('span');
-
var allKeywords = theKeywords.length;
-
if (this.getAttribute('value') == 'Highlight Keywords') {
-
for (var i=0; i<allKeywords; i++) {
-
theKeywords[i].className = 'highlightword';
-
}
-
this.setAttribute('value','No Keyword Highlights');
-
}
-
else {
-
for (var i=0; i<allKeywords; i++) {
-
theKeywords[i].className = '';
-
}
-
this.setAttribute('value','Highlight Keywords');
-
}
-
},
-
-
// modify text highlights based on the input button value
-
highlightSelections : function() {
-
-
var paragraphs = articleHolder.getElementsByTagName('p');
-
var allParas = paragraphs.length;
-
if (this.getAttribute('value') == 'Highlight Text on Hover') {
-
for (var i=0; i<allParas; i++) {
-
paragraphs[i].onmouseover = function() {
-
this.className = 'highlightpara';
-
}
-
paragraphs[i].onmouseout = function() {
-
this.className = '';
-
}
-
}
-
this.setAttribute('value','No Text Highlights');
-
}
-
else {
-
for (var i=0; i<allParas; i++) {
-
paragraphs[i].onmouseover = null;
-
paragraphs[i].onmouseout = null;
-
}
-
this.setAttribute('value','Highlight Text on Hover');
-
}
-
-
},
-
-
// modify the display of quotations based on the input button value
-
alterQuoteDisplay : function() {
-
-
var blockquotes = articleHolder.getElementsByTagName('blockquote');
-
for (var i=0, allBlockquotes = blockquotes.length; i<allBlockquotes; i++) {
-
if (this.getAttribute('value') == 'Hide Quotations') {
-
blockquotes[i].className = 'hide';
-
this.setAttribute('value','Show Quotations');
-
}
-
else {
-
blockquotes[i].className = '';
-
this.setAttribute('value','Hide Quotations');
-
}
-
}
-
},
-
-
// creates a list of all keywords and the removes any dublicates and printes the results
-
getKeywords : function() {
-
-
var keywords = articleHolder.getElementsByTagName('span');
-
var savedKeywords = [];
-
for (var i =0, allKeywords = keywords.length; i<allKeywords; i++) {
-
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
-
}
-
-
if (allKeywords > 0) {
-
var para = document.createElement('p');
-
var strong = document.createElement('strong');
-
para.appendChild(strong).appendChild(document.createTextNode('Keywords: '));
-
-
savedKeywords.sort();
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
-
savedKeywords.splice(n,1);
-
-
}
-
else {
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
}
-
}
-
-
-
}
-
-
articleHolder.insertBefore(para, document.getElementById('copyright'));
-
},
-
-
addEvent : function(obj, type, func) {
-
if (obj.addEventListener) {obj.addEventListener(type, func, false);}
-
else if (obj.attachEvent) {
-
obj["e" + type + func] = func;
-
obj[type + func] = function() {obj["e" + type + func] (window.event);}
-
obj.attachEvent("on" + type, obj[type + func]);
-
}
-
else {obj["on" + type] = func;}
-
}
-
}
-
-
pageOptions.addEvent(window, 'load', pageOptions.init);
I think that using savedKeywords.splice(n,1) in the for loop is not appropriate because the array length and then element position are changed after each duplicate deletion.
I'm looking further to find a workaround.
I think that using savedKeywords.splice(n,1) in the for loop is not appropriate because the array length and then element position are changed after each duplicate deletion.
I'm looking further to find a workaround.
Ok, But should I not get an error. The code loops passed the if condition and does the else condition. I've tried using a temp variable to copy the array and then compare the two, but the condition for the if is never met. Thanks for any help.
savedKeywords[i] = new Array(articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue);
This statement assigns to the element at position i of savedKeywords array a new object of type Array, that is a pointer to a string.
In this case, to compare the strings you can use:
if (savedKeywords[n][0] == savedKeywords[n + 1][0])
OR, remove "new Array" usage, and assign directly the value of the span:
savedKeywords[i] = articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue;
Then, in order to remove duplicate elements you could create a new Array that is filled in the "else" section of the "for" loop, after checking if the last added value is different from the current one.
OR, remove "new Array" usage, and assign directly the value of the span:
savedKeywords[i] = articleHolder.getElementsByTagName('span')[i].firstChild.nodeValue;
Then, in order to remove duplicate elements you could create a new Array that is filled in the "else" section of the "for" loop, after checking if the last added value is different from the current one.
I've removed new Array from the savedKeyword array, But I'm not clear about about creating the else for the for loop. Could you give me an example?
I mean the "else" section where apeears the line:
para.appendChild(document.createTextNode(savedKeyw ords[n]));
See my changes bellow: -
savedKeywords.sort();
-
var savedKeywordsNew = [];
-
var lastValue = "";
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
//savedKeywords.splice(n,1);
-
-
}
-
else {
-
If (lastValue != savedKeywords[n]){
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
savedKeywordsNew[savedKeywordsNew.length] = savedKeywords[n];
-
lastValue = savedKeywords[n];
-
}
-
}
-
}
-
-
I mean the "else" section where apeears the line:
para.appendChild(document.createTextNode(savedKeyw ords[n]));
See my changes bellow: -
savedKeywords.sort();
-
var savedKeywordsNew = [];
-
var lastValue = "";
-
for ( var n = 0, allSaved = savedKeywords.length; n <allSaved; n++) {
-
alert(savedKeywords[n]);
-
if (savedKeywords[n] == savedKeywords[n + 1]) {
-
//savedKeywords.splice(n,1);
-
-
}
-
else {
-
If (lastValue != savedKeywords[n]){
-
para.appendChild(document.createTextNode(savedKeywords[n]));
-
para.appendChild(document.createTextNode(', '));
-
savedKeywordsNew[savedKeywordsNew.length] = savedKeywords[n];
-
lastValue = savedKeywords[n];
-
}
-
}
-
}
-
-
thanks, that did it.
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
I'm sure that exist methods without sort, but they would be more complex.
If you really need, I will research for that deeper.
One last question if you don't mind. How would you create a loop to remove duplicates without using sort?
Well, it should be possible, but it won't be as efficient. You can have two arrays. One contains the words, the second one is empty. Then as you loop through the first array, you call a function which checks in the second array (has the word already been added). If the word is already present it is ignored, otherwise it is added. It should be quite simple to implement.
- Array.prototype.hasAny= function(wot){
-
var L= this.length, cnt= 0;
-
for(var i= 0; i< L; i++){
-
if(A[i]=== wot) ++cnt;
-
}
-
return cnt;
-
}
-
Array.prototype.unique= function(){
-
var A= [];
-
var L= this.length;
-
for(var i= 0; i< L; i++){
-
var tem= this[i];
-
if(A.hasAny(tem)== 0) A.push(tem);
-
}
-
return A;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alex Hopson |
last post by:
Hi,
I'm trying to modify a shopping cart script from Mastering PHP/MySQL and am
having trouble setting up some arrays for it. The original code, below,
stores the cart items in a session...
|
by: Andre Ranieri |
last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
|
by: DaHool |
last post by:
Hi there !!!
I browsed around the Internet in search for a solution of a little difficult
problem i have in VB.NET....
However, i cannot find a suitable anwser anywhere, so i thought i'll give...
|
by: 4Ankit |
last post by:
hey guys i am having trouble changing my array code to include a 'for/
in' structure
the code i am trying to change is below:
<script type="text/javascript">
var contents = new Array(3)
|
by: _Skare_Krow_ |
last post by:
I have a database with three columns. One is an atomic number, one is a
pic url, and the other is a description. What I'm having trouble doing
is putting the url in one table row and the...
|
by: Nathan Sokalski |
last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action
that I want to perform on each of the Array values is:
Private Function AddQuotes(ByVal value As String) As String
Return...
|
by: bmerlover |
last post by:
This code makes sense to me, I'm just having trouble trying to understand why it doesn't work correctly. This is a GUI APP. When the Play button is Clicked, the play_Click(System::Object * sender,...
|
by: jthep |
last post by:
Hi,
I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in...
|
by: Michellevt |
last post by:
Hi
I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |