inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 21, 2016 0:38:25 GMT -8
Again only used on custom pages - so I know not a plugin but I know not else where to place this!
function LandBounty (name, image, wantedLevel, reward) { this.name = name; this.image = image; this.wantedLevel = wantedLevel; this.reward = reward; }
function NavalBounty (name, image, wantedLevel, reward) { this.name = name; this.image = image; this.wantedLevel = wantedLevel; this.reward = reward; }
LandBounty.prototype.toHTML = function(question) { var htmlString = question; return htmlString; };
function LandOutput (){ this.landList = [ ]; } LandOutput.prototype.add = function(newlandList) { this.landList.push(newlandList); };
var landBountyList = new LandOutput ();
var bountyland = new LandBounty ("TestymcGee","http://placehold.it/128x128","2","£5000"); var bountyland2 = new LandBounty ("TestyMcCreed","http://placehold.it/128x128","1","£1000");
var bountyNaval = new NavalBounty ("TestymcGee","http://placehold.it/128x128","2","£6500"); var bountyNaval2 = new NavalBounty ("TestyMcCreed","http://placehold.it/128x128","1","£2000");
landBountyList.add(bountyland); landBountyList.add(bountyland2);
console.log(landList.length)
This is so far with my testingness -
I'm TRYING to push the information into an array for for looping through, apparently my mind is struggling to make this work. Doing more work on it but if anyone can push me in the right direction
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jun 21, 2016 13:53:00 GMT -8
Did you check the console? You will see that landList is not defined. It is not in the global scope. I'ts not in any scope. It's a property of an object you are inserting into the add method of landBountyList. It can be accessed in the add method. It can also be accessed by specifying the object and property in the global scope.
bountyland.landList.length
Oh and I just have to say; You know LandBounty and NavalBounty are the same? You could just use one and call it bounty
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 21, 2016 20:57:07 GMT -8
I need to call two them eventually to two seperate lists
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 21, 2016 21:23:12 GMT -8
Still getting "Cannot read property 'length' of undefined'
However when I load the function in and type in 'landBountyList' it then tells me it has an array of two
$( document ).ready(function() {
function Bounty (name, image, wantedLevel, reward) {
this.name = name;
this.image = image;
this.wantedLevel = wantedLevel;
this.reward = reward;
}
function Output (){
this.Listing = [ ];
}
Output.prototype.add = function(newList) {
this.Listing.push(newList);
};
var landBountyList = new Output ();
var bountyland = new Bounty ("TestymcGee","http://placehold.it/128x128","2","£5000");
var bountyland2 = new Bounty ("TestyMcCreed","http://placehold.it/128x128","1","£1000");
var bountyNaval = new Bounty ("TestymcGee","http://placehold.it/128x128","2","£6500");
var bountyNaval2 = new Bounty ("TestyMcCreed","http://placehold.it/128x128","1","£2000");
landBountyList.add(bountyland);
landBountyList.add(bountyland2);
var htmlStringLand;
for(i = 0; i < landBountyList.length; i++) {
htmlStringLand = '<div class="box wanted"><span class="has-text-centered">';
htmlStringLand += name;
htmlStringLand += '</span><br /><p class="is-128"><img src="';
htmlStringLand += image;
htmlStringLand += '" /></p> <br /><strong>Wanted Level</strong> ';
htmlStringLand += wantedLevel;
htmlStringLand += '<br /><strong>Reward</strong>';
htmlStringLand += reward;
}
$("#land-bountys").append(htmlStringLand);
});
Updated list...that's showing no errors but not outputting me anything either
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jun 22, 2016 6:38:16 GMT -8
landBountyList is not an array. It's an object that has inherited from Output. Output has the property that you are looking for, which is inherited to landBountyList. The array you are looking for is a property on landBountyList.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 22, 2016 7:14:23 GMT -8
Okay so would I be using output.length?
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jun 22, 2016 8:00:37 GMT -8
I don't think you know what a property is. It is a "variable" on an object. You are assigning the Listing property onto the objects that you create, due to prototypal inheritance. So you need to read the Listing property on that variable. You had the right variable in your example, you just were not accessing the Listing property from that object.
Then there's the HTML inside the loop itself. There is no name, image etc variable. They are all properties of the objects inside the array you're trying to access. you need to access the property of the objects.
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 22, 2016 9:54:25 GMT -8
Okay so let me get this straight - LandBountyList is a property? Or a method?
Listing counts as a variable for the object (aka a property).
So as my head is hurting do I need to check against Listings Length or Listings.landBountyList ? Okay another code update as well
$( document ).ready(function() {
function Bounty (name, image, wantedLevel, reward) {
this.name = name;
this.image = image;
this.wantedLevel = wantedLevel;
this.reward = reward;
}
function Output (){
this.Listing = [ ];
}
Output.prototype.add = function(newList) {
this.Listing.push(newList);
};
var landBountyList = new Output ();
var bountyland = new Bounty ("TestymcGee","http://placehold.it/128x128","2","£5000");
var bountyland2 = new Bounty ("TestyMcCreed","http://placehold.it/128x128","1","£1000");
var bountyNaval = new Bounty ("TestymcGee","http://placehold.it/128x128","2","£6500");
var bountyNaval2 = new Bounty ("TestyMcCreed","http://placehold.it/128x128","1","£2000");
landBountyList.add(bountyland);
landBountyList.add(bountyland2);
var htmlStringLand;
for(i = 0; i < Output.Listing.length; i++) {
console.log([i]);
htmlStringLand = '<div class="box wanted"><span class="has-text-centered">';
htmlStringLand += name;
htmlStringLand += '</span><br /><p class="is-128"><img src="';
htmlStringLand += image;
htmlStringLand += '" /></p> <br /><strong>Wanted Level</strong> ';
htmlStringLand += wantedLevel;
htmlStringLand += '<br /><strong>Reward</strong>';
htmlStringLand += reward;
}
$("#land-bountys").append(htmlStringLand);
});
Have I not found the right 'key' to use for the property yet?
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jun 22, 2016 13:49:40 GMT -8
Use your previous example, not the recent one, and put this before the loop. tell me what you see. console.log(landBountyList);
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 22, 2016 21:50:59 GMT -8
In the Console I see Output when expanded I see Listing: Array [2]
|
|
inherit
216224
0
Aug 1, 2024 1:18:46 GMT -8
Quozzo
408
November 2014
quozzo
|
Post by Quozzo on Jun 23, 2016 3:47:39 GMT -8
Isn't that the array you were looking for?
|
|
inherit
Peabrained Codebreaker
107114
0
Mar 11, 2020 7:47:27 GMT -8
Boy_Wonder
6,249
July 2007
natzy24
|
Post by Boy_Wonder on Jun 23, 2016 4:09:32 GMT -8
Yes - as long as I can target it to only that which is pushed into it by LandBounty versus the next list I'll be doing which is Navalbountylists
|
|