Ayah's Students explainer

Aim of the puzzle: Find the key information in a word problem and solve the problem using code.
Walk through of solution: Ayah needs to figure out how many students in her class will need an adult ticket for their trip to the museum. The museum states that all visitors older than 12 must have an adult ticket.

In this puzzle, the studentList is imported from additional code. It contains an array of objects, each with a name and age property.

The function will need to be called with the studentList, so to start, add a parameter to the needsAdultTicket() function, for example, needsAdultTicket(list).

Then, inside the function, add a for loop to iterate through the parameter. Then, add an if statement that checks if the age property of each item in the array is greater than 12.

Inside the if statement, use console.log() to print the name property of the students who are older than 12.

Finally, call the function with studentList as an argument.
Sample code solution:

import { studentList } from 'class.students';

function needsAdultTicket(list) {
    for (let element of list) {
        if (element['age'] > 12) {
            console.log(element['name']);
        }
    }
}

needsAdultTicket(studentList);

Additional Code:

studentList: [
            {name:'Amelia',     age: 13},
            {name:'Anna' ,      age: 12},
            {name:'Charlotte',  age: 13},
            {name:'Claudia',    age: 13},
            {name:'Clyde',      age: 13},
            {name:'Emily',      age: 12},
            {name:'Jack',       age: 12},
            {name:'Kyle',       age: 13},
            {name:'Marcus',     age: 13},
            {name:'Rachel',     age: 12},
            {name:'Tony',       age: 12},
        ]

Javascript Concepts: Functions, Arrays, For Loops, If Statements, Objects, Properties

What am I doing wrong?

Still cant change “for” statement from “var” to “list.” Any suggestions?

Hey there,

The first part of a for loop (the var element) sets up a temporary looping variable that represents the current item each time it loops. The of studentList tells the for loop what to loop through.

This means you don’t have to change the 1st part of the for loop when you add it to the code in Grasshopper. The var element that is automatically added to the code is fine as is. You just need to change the 2nd part so the loop knows what to iterate through.

Hope this helps!
Ben