Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

Kevin Ohlsson
Kevin Ohlsson
4,559 Points

students.lenght = undefined

Hello

this does not work:

for (let i = 0; i < students.lenght; i++) {
    let stuObj = students[i];
    let datas = `<h1>${stuObj.name}</h1>`
    datas += `<p>${stuObj.track}</p>`
    datas += `<p>${stuObj.achievements}</p>`
    datas += `<p>${stuObj.points}</p>`
    print(datas)
    }

function print(arg) {
    document.write(`<div> ${arg} </div>`)
}

but this does

for (let i = 0; i < 5; i++) {
    let stuObj = students[i];
    let datas = `<h1>${stuObj.name}</h1>`
    datas += `<p>${stuObj.track}</p>`
    datas += `<p>${stuObj.achievements}</p>`
    datas += `<p>${stuObj.points}</p>`
    print(datas)
    }

function print(arg) {
    document.write(`<div> ${arg} </div>`)
}

why does students.lenght return undefined?

let students = [
    {   
        name: "Kevin",
        track: "JavaScript",
        achievements: "123",
        points: 3500,
    },
    {   
        name: "John",
        track: "LUA",
        achievements: "312",
        points: 2121
    }]

3 Answers

Gia Gabadadze
Gia Gabadadze
1,794 Points

you are using students.lenght you should use students.length

for (let i = 0; i < students.length; i++) {
    let stuObj = students[i];
    let datas = `<h1>${stuObj.name}</h1>`
    datas += `<p>${stuObj.track}</p>`
    datas += `<p>${stuObj.achievements}</p>`
    datas += `<p>${stuObj.points}</p>`
    print(datas)
    }

function print(arg) {
    document.write(`<div> ${arg} </div>`)
}

Try students.length (notice the 's')

Kevin Ohlsson
Kevin Ohlsson
4,559 Points
undefined
for (let i = 0; i < students.lenght; i++) {
    let stuObj = students[i];
    let datas = `<h1>${stuObj.name}</h1>`
    datas += `<p>${stuObj.track}</p>`
    datas += `<p>${stuObj.achievements}</p>`
    datas += `<p>${stuObj.points}</p>`
    print(datas)
    }

function print(arg) {
    document.write(`<div> ${arg} </div>`)
}

not misspell

Kevin Ohlsson
Kevin Ohlsson
4,559 Points
Uncaught ReferenceError: student is not defined