0
0
Angularframework~20 mins

map operator for transformation in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RxJS Map Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output of this Angular component using map operator?

Consider this Angular component that uses RxJS map operator to transform data from an observable. What will be displayed in the template?

Angular
import { Component } from '@angular/core';
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
  selector: 'app-transform',
  template: `<div>{{ transformedValue | async }}</div>`
})
export class TransformComponent {
  source$ = of(5);
  transformedValue = this.source$.pipe(
    map(x => x * 3)
  );
}
AObservable { }
B5
C15
Dundefined
Attempts:
2 left
💡 Hint

Remember that map transforms the emitted value before it reaches the template.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses the map operator to transform an observable of strings to their lengths?

Given an observable emitting strings, which code snippet correctly uses map to transform each string to its length?

Angular
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

const source$ = of('apple', 'banana', 'cherry');
const lengths$ = source$.pipe(
  // transformation here
);
Amap(str => str.length)
Bmap(str => str.toUpperCase())
Cmap(str => str.length())
Dmap(str => str.length + 1)
Attempts:
2 left
💡 Hint

Check how to get the length of a string in JavaScript.

🔧 Debug
advanced
2:00remaining
Why does this Angular observable transformation cause a runtime error?

Examine the following code snippet. Why does it cause a runtime error when subscribed?

Angular
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

const source$ = of(null);
const transformed$ = source$.pipe(
  map(x => x.toString())
);

transformed$.subscribe(console.log);
ABecause <code>subscribe</code> is missing a callback function
BBecause <code>map</code> operator is not imported correctly
CBecause <code>of(null)</code> does not emit any value
DBecause <code>x</code> is null and calling <code>toString()</code> on null causes a TypeError
Attempts:
2 left
💡 Hint

Think about what happens when you call a method on null.

state_output
advanced
2:00remaining
What is the final emitted value after applying multiple map operators?

Given this observable pipeline, what is the final value emitted?

Angular
import { of } from 'rxjs';
import { map } from 'rxjs/operators';

const source$ = of(2);
const result$ = source$.pipe(
  map(x => x + 3),
  map(x => x * 2),
  map(x => `Value: ${x}`)
);

result$.subscribe(console.log);
AValue: 7
BValue: 10
CValue: 5
DValue: 4
Attempts:
2 left
💡 Hint

Apply each map transformation step by step.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the behavior of the map operator in Angular's RxJS?

Choose the most accurate description of how the map operator works in Angular's RxJS library.

AIt transforms each emitted value from the source observable by applying a given function, emitting the transformed value.
BIt combines multiple observables into one by merging their emissions.
CIt filters out values from the source observable that do not satisfy a condition.
DIt delays the emission of values from the source observable by a specified time.
Attempts:
2 left
💡 Hint

Recall the purpose of map in functional programming and RxJS.