Challenge - 5 Problems
NestJS Service Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS service method call?
Consider this NestJS service method that returns a greeting message. What will be the output when
getGreeting() is called?NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class GreetingService { getGreeting(): string { return `Hello, NestJS!`; } } const service = new GreetingService(); console.log(service.getGreeting());
Attempts:
2 left
💡 Hint
Look at the return statement inside the getGreeting method.
✗ Incorrect
The getGreeting method returns the string 'Hello, NestJS!'. When called, it outputs exactly that string.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a NestJS service with dependency injection?
You want to create a NestJS service
OrderService that depends on PaymentService. Which option correctly injects PaymentService into OrderService?Attempts:
2 left
💡 Hint
Look for the correct use of @Injectable and constructor parameter visibility.
✗ Incorrect
Option C uses @Injectable decorator and declares the dependency in the constructor with 'private' keyword, which automatically assigns it as a class property and enables NestJS to inject it.
🔧 Debug
advanced2:00remaining
Why does this NestJS service fail to inject the dependency?
Given the following code, why will NestJS fail to inject
UserRepository into UserService?NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class UserService { constructor(private userRepository: UserRepository) {} } @Injectable() export class UserRepository {}
Attempts:
2 left
💡 Hint
Check if all injectable classes have the @Injectable decorator.
✗ Incorrect
NestJS requires all injectable classes to have the @Injectable decorator to register them in the dependency injection system. UserRepository lacks this decorator, so injection fails.
❓ state_output
advanced2:00remaining
What is the value of
count after calling increment() twice?Consider this NestJS service that tracks a count. What will be the value of
count after calling increment() two times?NestJS
import { Injectable } from '@nestjs/common'; @Injectable() export class CounterService { private count = 0; increment() { this.count++; } getCount() { return this.count; } } const service = new CounterService(); service.increment(); service.increment(); const result = service.getCount();
Attempts:
2 left
💡 Hint
Each call to increment increases count by one.
✗ Incorrect
The count starts at 0. Each increment call adds 1. After two calls, count is 2.
🧠 Conceptual
expert2:00remaining
Which statement best describes the role of services in NestJS?
In NestJS, what is the primary purpose of a service?
Attempts:
2 left
💡 Hint
Think about where the core logic of your app should live.
✗ Incorrect
Services in NestJS are designed to hold business logic and reusable code, separate from controllers which handle HTTP requests.