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

1 Like

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

Hi, I’m still stuck in here. My code looks working fine. I don’t know why I am told my coding is wrong. Please check my attached coding.

Hey there,

It looks like this issue is that the function call is inside a console.log(). This means the puzzle’s completion logic isn’t finding the function call, which is why you’re seeing a message indicating that the function call doesn’t exist.

Try taking the function call out of the console.log(), and the puzzle should complete.

Hope this helps!
Ben

1 Like

Hey there, it looks like the ['name'] is outside the console.log() statement.

Try changing:

console.log(element)['name']

to

console.log(element['name'])

Hope this helps!
Ben

Screenshot_20190914-133751_Grasshopper

I’m not sure what I’m doing wrong. Please help! I’ve tried the function and calling it but I cant figure out what I need to fix.

Hey there, the puzzle’s completion trigger is looking for the function call needsAdultTicket(studentList), but can’t see it because the call is inside a console.log().

Try changing console.log(needsAdultTicket(studentList)) to just needsAdultTicket(studentList).

That should solve the problem! Let me know if you have any questions.
Ben

Focus… Is reading and comprehension…

I know you need to feed a parameter into the function to use the imported studentList array into the function, but it’s never mentioned in the instructions making it look like all the code needs to be done within the function.

Maybe addressing this in the instructions would make this more clearer for others?

Hey there, great feedback!

Thanks,
Ben