Challenge: MVVM Pattern
In this challenge, you have to implement the MVVM pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you need to use the MVVM pattern to change the color of the “color” you write in the input field.
In the Model
, you need to define the following functions:
-
subscribe
: Registers an observer. -
notifyObservers
: Notifies the observer of a change. -
getCurrentColor
: Returns the current color. -
setColor
: Sets a new color.
In the ViewModel
, you need to define the bind
function, which reflects changes made in the view on the model and vice versa. It should do the following as well:
-
If you write
green
in the input, the color of the textgreen
should change tored
. -
If you write
red
in the input, the color of the textred
should change toblue
. -
If you write
blue
in the input, the of the textblue
should change togreen
.
The view is provided to you in the form of the HTML code. Study it carefully before writing the JavaScript code.
Input
The name of the color
Output
The color of the text green, red, or blue changes according to the rules given
Sample input
green
red
blue
Sample output
green
red
blue
Challenge #
Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
Let’s discuss the solution in the next lesson.