Challenge: MVVM Pattern
In this challenge, you have to implement the MVVM pattern to solve the given problem.
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
greenin the input, the color of the textgreenshould change tored. -
If you write
redin the input, the color of the textredshould change toblue. -
If you write
bluein the input, the of the textblueshould 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.