Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Kevin Ohlsson
Kevin Ohlsson
4,547 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,547 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,547 Points
Uncaught ReferenceError: student is not defined