Need help with part of a function.

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1089 Views - Last Post: 26 July 2014 - 09:16 PM

#1 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Need help with part of a function.

Posted 23 July 2014 - 07:28 PM

Currently I have an error with this line but that is how you set the date. What did I miss. I am sure there are more errors but the error console is stuck on the new Date undefined.
Lastly how would you append a child to an array? Last line of code does that look right. I put some comments where there might be an issue or what is needed to be done

Thanks for the help!

 lastVisit = newDate("August 13, 2013 13:00:00");




function highlightNew() {
  
//test if lastVisit is equal to null 5 if not
    if (getCookie("lastVisit") != null) {
        

//create a variable lastVisit that contins dateObject using the value of lastVisit cookie 
//otherwise set the value  of the last variable to null;
        var lastVisit = new Date("lastVisit");
    } else {
        lastVisit = null; 
    }
        
//Retrieve and store the time in the last visit cookie using writeDateTime function to format
//for this project set time as  13-Aug-2013 13:00:00 GMT

    var lastVisit = newDate("August 13, 2013 13:00:00");
    //store the time
    lastVisit = writeDateTime(lastVisit);

        var lastDate = document.createElement("span");
        lastDate.id = lastDate;
        htmlString = "<span id= 'lastDate'>";
        //htmlString = "<span id=" +lastDate+ ">"; 
        htmlString += "You last visited on " + lastVisit;
        htmlString += "</span>";
        lastDate.innerHTML = htmlString;
        document.body.appendChild(lastDate);
    
//locate all elements of class name posttime
//place them in allStories Array
    var allStories = new Array();
    allStories = document.getElementById("p").getElementsByClassName("posttime");
    for (var i =0; i < allStories.length; i++) {
        
//Retrieve the the text of the item and slice the text string, starting at the ninth character
//to retain the date posted
//create a dateObject variable storyDate
        var storyDate = new Date();
        storyDate = allStories.substring(9);
        if (lastVisit < storyDate) {
        
    //if it indicates that the user last visited the Web site before the story was posted 
    //create an html element with an inline image
       allStories.innerHTML += ('<img src = "' +newitem.jpg+ '"/>');
//append the inline image as a child of the all stories array
        document.body.appendChild(allStories);}
}
    }





Is This A Good Question/Topic? 0
  • +

Replies To: Need help with part of a function.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4405
  • View blog
  • Posts: 12,262
  • Joined: 18-April 07

Re: Need help with part of a function.

Posted 23 July 2014 - 10:29 PM

new Date("lastvisit")???? I think you wanted to set a variable to the value returned by getCookie and then use that variable in the Date constructor.... but that is not what you are doing. You are just passing a string to the date object.

Also you are using newDate("August 13, 2013 13:00:00");. I think you need a space between "new" and "Date". You are wanting to create a new Date object right? Or is that some custom defined function you created?
Was This Post Helpful? 1
  • +
  • -

#3 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 471
  • View blog
  • Posts: 1,729
  • Joined: 15-January 14

Re: Need help with part of a function.

Posted 24 July 2014 - 10:35 AM

If you want to add a new item to an array you can use Array.push.

var ar = new Array();
ar.push("1");
ar.push("2");
ar.push("3");
alert(ar.join(","));

Was This Post Helpful? 1
  • +
  • -

#4 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 24 July 2014 - 07:31 PM

I didn't catch the missing space in the new Date() function. Thanks. Also how would I do create this as I don't think it is correct.



//locate all elements of class name posttime
//place them in allStories Array
    var allStories = new Array();
    allStories = document.getElementsByClassName("posttime");
    for (var i =0; i < allStories.length; i++) {
        
//Retrieve the the text of the item and slice the text string, starting at the ninth character
//to retain the date posted
//create a dateObject variable storyDate
        var storyDate = new Date(allStories.substring(9));
        


Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 471
  • View blog
  • Posts: 1,729
  • Joined: 15-January 14

Re: Need help with part of a function.

Posted 25 July 2014 - 08:55 AM

I don't know what you're asking. Why don't you think that is correct?
Was This Post Helpful? 0
  • +
  • -

#6 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 25 July 2014 - 05:37 PM

I get this error in the console

'undefined' is not a function (evaluating 'allStories.substring(9)') unless this is also incorrect.


allStories = document.getElementsByClassName("posttime");




also for the cookie to have value would this be the way


getCookie("lastVisit", document.lastVisit.value);




or I am totally heading in the wrong direction?
Was This Post Helpful? 0
  • +
  • -

#7 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 25 July 2014 - 07:28 PM

So this is what I have so far. The cookie part that shows last visit date works as it displays the time of last visit. The rest is still not quite there. This is what I have so far. I think the issue is with the string slice. Also at the end how would I push an image into array. I understand the push method, but how do i go about doing so?

function highlightNew() {

    if (getCookie("lastVisit") != null) {
        

//create a variable lastVisit that contins dateObject using the value of lastVisit cookie 
//otherwise set the value  of the last variable to null;
        var lastVisit = new Date("lastVisit");
    } else {
        lastVisit = null; 
    }
        
//Retrieve and store the time in the last visit cookie using writeDateTime function to format
//for this project set time as  13-Aug-2013 13:00:00 GMT

    var lastVisit = new Date("August 13, 2013 13:00:00");
    //store the time
    lastVisit = writeDateTime(lastVisit);
       // lastVisit = lastVisit.toGMTString();
//write the following HTML if lastVisit is not equal to null
        var lastDate = document.createElement("span");
        lastDate.id = lastDate;
        htmlString = "<span id= 'lastDate'>";
        //htmlString = "<span id=" +lastDate+ ">"; 
        htmlString += "You last visited on " + lastVisit;
        htmlString += "</span>";
 //append lastDate to document
        lastDate.innerHTML = htmlString;
        document.body.appendChild(lastDate);
    
//locate all elements of class name posttime
//place them in allStories Array
    var allStories = new Array();
    allStories = document.getElementsByClassName("posttime");
    for (var i =0; i < allStories.length; i++) {
        
//Retrieve the the text of the item and slice the text string, starting at the ninth character
//to retain the date posted located under p class=posttime html
//create a dateObject variable storyDate
        var storyDate = allStories[i].slice(9);
        
        if (lastVisit < storyDate) {
        
   //     for each story test the variable lastVisit against storyDate
    //if it indicates that the user last visited the Web site before the story was posted 
    //create an html element with an inline image
      // allStories.innerHTML += ('<img src = "' +newitem.jpg+ '"/>');
      //  document.body.appendChild(allStories);}
        //    var newItem = document.getElementById("image").src = "newitem.jpg";
            allStories.push(newItem);
}
    }
}





Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3745
  • View blog
  • Posts: 13,108
  • Joined: 12-December 12

Re: Need help with part of a function.

Posted 26 July 2014 - 02:02 AM

new Date("lastVisit");

This is still incorrect as already mentioned. The string "lastVisit" is not a valid value to pass to the Date() constructor.

Perhaps you meant
new Date(getCookie("lastVisit"));


But on line 16 you overwrite this variable (lastVisit) anyway.

..and again on line 18.
Was This Post Helpful? 1
  • +
  • -

#9 Blindman67  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 133
  • View blog
  • Posts: 590
  • Joined: 15-March 14

Re: Need help with part of a function.

Posted 26 July 2014 - 04:04 AM

document.getElementsByClassName("posttime");


Does not return an array and thus does not have the slice method.

DOM queries return HTMLCollection objects that have a length and can use bracket and dot notation. It only has two methods .item(index) (get item by index 0,1,2...n) and .namedItem("ID") (get item by its ID)

See HTMLCollection for details.

If you wish to convert the HTMLCollection to and array you need to call the Array slice method.
var allStories= [].slice.call(document.getElementsByClassName("posttime")); // or some prefer
var allStories= Array.prototype.slice.call(document.getElementsByClassName("posttime"));


This will not work for older versions of IE. If you wish to be compatible then you will have to iterate and create the array. If you do this often it will pay to create a function to do the job.

var temp = document.getElementsByClassName("posttime"); // get the collection
var allStories = [];                                    // array to store items
for(var i = 0; i < temp.length; i++){                   // iterate each item.
    allStories.push(temp[i]);                           // push onto the array.
}
delete temp;                                            // clean up by dereference
// or 
temp = undefined;
// or
temp = null;

This post has been edited by Blindman67: 26 July 2014 - 04:09 AM

Was This Post Helpful? 1
  • +
  • -

#10 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 26 July 2014 - 04:23 PM

Ok there are no more errors but how to add the inline image into the article if the article is new. I think i got the push method down but the image itself? Its in a file by itself. This is what I have, drawing a blank at the end. Thanks




function highlightNew() {

    if (getCookie("lastVisit") != null) {

    var lastVisit = new Date(getCookie("lastVisit"));
  } else {
        lastVisit = null; 
   }
    var lastVisit = new Date("August 13, 2013 13:00:00");
    lastVisit = writeDateTime(lastVisit);
    
    var lastDate = document.createElement("span");
    lastDate.id = lastDate;
 
        htmlString = "<span id= 'lastDate'>";
        htmlString += "You last visited on " + lastVisit;
        htmlString += "</span>";

        lastDate.innerHTML = htmlString;
        document.body.appendChild(lastDate);
    
         var allStories = new Array();
    var allStories = Array.prototype.slice.call(document.getElementsByClassName("posttime"));
    for (var i =0; i < allStories.length; i++) {
        
 var storyDate = allStories.slice(9);
        
    if (lastVisit < storyDate) {
    //allStories.innerHTML +=('img src = "'+newitem.jpg+'"/>');
       // document.body.appendChild(allStories);
       // if (lastVisit == null) 
       // lastVisit = null;
        //var newItem = <img src = "newitem.jpg"/>
        allStories.push(newItem);
}
    }
}





Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3745
  • View blog
  • Posts: 13,108
  • Joined: 12-December 12

Re: Need help with part of a function.

Posted 26 July 2014 - 05:14 PM

You need to describe what you are attempting to do clearly.

Quote

how to add the inline image into the article if the article is new.

What article, or articles? What are they? What image? Does it need to be inline? etc..

(We aren't standing next to you looking at your screen ;))

This post has been edited by andrewsw: 26 July 2014 - 05:15 PM

Was This Post Helpful? 1
  • +
  • -

#12 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 26 July 2014 - 05:27 PM

I need to append an inline image as a child of an array. <img src="new item.jpg" />
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3745
  • View blog
  • Posts: 13,108
  • Joined: 12-December 12

Re: Need help with part of a function.

Posted 26 July 2014 - 05:36 PM

View Postssbodyrock, on 27 July 2014 - 12:27 AM, said:

I need to append an inline image as a child of an array

An array doesn't have children.

allStories is an array - neither does it have an innerHTML.

You'll probably want/need to work with the original document.getElementsByClassName("posttime"). If you will be appending an img-element to all of these elements then you'll need to iterate this collection, appending one at a time.
Was This Post Helpful? 0
  • +
  • -

#14 ssbodyrock  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 08-June 14

Re: Need help with part of a function.

Posted 26 July 2014 - 05:37 PM

So what the assignment is asking for cannot be done? Can an image be pushed into Array?
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3745
  • View blog
  • Posts: 13,108
  • Joined: 12-December 12

Re: Need help with part of a function.

Posted 26 July 2014 - 05:45 PM

An alternative approach is to consider the :after pseudo element, just using Javascript to change (or add) the class-name to one that uses ':after'. (I'm kinda assuming that 'new item.jpg' is a small icon. Note that web-file-names should not contain spaces.)

Or possibly as a background image, positioned to the bottom-right corner, and again using JS to switch classes.

I'm just mentioning these as possibilities, you can add an img if it suits.

View Postssbodyrock, on 27 July 2014 - 12:37 AM, said:

So what the assignment is asking for cannot be done? Can an image be pushed into Array?

Yes, an image can be pushed into an array (haven't you done something similar already?). But then I'm no longer sure what you are trying to achieve, your previous posts suggested to me that you are trying to insert an image into the page(?).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2