/    Sign up×
Community /Pin to ProfileBookmark

Array help – beginner!

<!DOCTYPE html>
<html>
<body>

<p>Creating and using an object method</p>

<p id=”demo”></p>

<script>
function car(make,model,colour) {
this.make = make;
this.model = model;
this.colour = colour;
this.changeCarMake = function (newCarMake) {
this.make = newCarMake;
}
}
var car1 = new car(“Ford”,”Focus”,”Black”);
car1.changeCarMake(“BMW”);
document.getElementById(“demo”).innerHTML =
“My new car is a ” + car1.make;
</script>

</body>
</html>

I have the following code which uses a custom object. Can anyone add array code which makes use of this custom object?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@PingPingSep 09.2016 — var arr = [];

arr.push(new car("VW", "Beatle", "Hot Pink"));

arr.push(car1);

var i;

for (i=0; i < arr.length; i = i + 1) {

console.log(arr[i].make); VW, BMW

}
Copy linkTweet thisAlerts:
@Coder_Cook100authorSep 09.2016 — Thank you, tried to embed the code into mine (pasted code below). Can't get it to output. Can you help / see what problem is?


<!DOCTYPE html>

<html>

<body>

<p id="demoArray"></p>

<script>

function car(make,model,colour) {

this.make = make;

this.model = model;

this.colour = colour;

this.changeCarMake = function (newCarMake) {

this.make = newCarMake;

}

}

var arr = [];

arr.push(new car("VW", "Beatle", "Hot Pink"));

arr.push(car1);

var i;

for (i=0; i < arr.length; i = i + 1) {

console.log(arr[i].make); // VW, BMW

}



document.getElementById("demoArray").innerHTML = "Car" + arr[i].make;

</script>



</body>

</html>
Copy linkTweet thisAlerts:
@rootSep 10.2016 — When dealing with DOM, you have to create a feature to roll up the data in to a formatted string and then push it to the DOM element or update the content + the new element.

Also...
<i>
</i>var i;
// pointless declaration, var i or var i = 0 or just use i if in the global scape, in your case, i is a global in the global namespace and does not need to be private unless it needs to be when its within a function, so declaring it as var is pointless.
<i>
</i>tmp = "";
for (i=0; i &lt; arr.length; i = i + 1) {
tmp += arr[i].make + "&lt;br&gt;";
}
document.getElementById("demoArray").innerHTML = "Car" + tmp;
Copy linkTweet thisAlerts:
@rootSep 10.2016 — Also, when you post any code, please use the forum tags as listed in my signature, it helps as you can see in my post above...
Copy linkTweet thisAlerts:
@SempervivumSep 10.2016 — 
  • 1. Variable car1 is undefined.

  • 2. Here:
    [CODE]document.getElementById("demoArray").innerHTML = "Car" + arr[i].make;[/CODE]
    the loop is finished and i has a value of 2 when there would be two elements in your array. This is beyond the boundaries of the array: arr[2] doesn't exist.


  • Try this:
    [CODE] <p id="demoArray"></p>

    <script>
    function car(make,model,colour) {
    this.make = make;
    this.model = model;
    this.colour = colour;
    this.changeCarMake = function (newCarMake) {
    this.make = newCarMake;
    }
    }
    var arr = [];
    arr.push(new car("VW", "Beatle", "Hot Pink"));
    arr.push(new car("Mazda", "3", "Silver"));
    var i;
    for (i=0; i < arr.length; i = i + 1) {
    console.log(arr[i].make); // VW, Mazda
    document.getElementById("demoArray").innerHTML += "Car " + (i + 1) + ": " + arr[0].make + "<br>";
    }
    </script>[/CODE]
    ×

    Success!

    Help @Coder_Cook100 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...