File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ const commandInput = document . querySelector ( ".commandField" ) ;
2+ const output = document . querySelector ( ".output" ) ;
3+ const outputContainer = document . querySelector ( ".command-output" ) ;
4+ const commands = [ "help" , "clear" ] ;
5+
6+ const executeCommand = ( cmd ) => {
7+ switch ( cmd )
8+ {
9+ case 'help' :
10+ console . log ( "Help cmd" ) ;
11+ break ;
12+ case 'clear' :
13+ console . log ( "Clear cmd" ) ;
14+ break ;
15+ }
16+ }
17+
18+ commandInput . addEventListener ( "keydown" , ( e ) => {
19+
20+ // get command from input box
21+ const inputCommand = commandInput . value . trim ( ) ;
22+
23+ if ( e . key === "Enter" && inputCommand !== '' )
24+ {
25+
26+ // create new element and append it on output
27+ const createElement = output . cloneNode ( true ) ;
28+ const outputTextMessage = createElement . querySelector ( ".outputText" ) ;
29+
30+ createElement . style . display = "block" ;
31+ outputTextMessage . textContent = inputCommand ;
32+ outputContainer . append ( createElement ) ;
33+
34+ // clear the input box
35+ commandInput . value = '' ;
36+
37+ if ( commands . includes ( inputCommand ) )
38+ executeCommand ( inputCommand ) ;
39+ else
40+ outputTextMessage . setHTML ( `<br> ${ inputCommand } Command not found. Type \'help\' for list of available commands.` ) ;
41+ }
42+ } ) ;
You can’t perform that action at this time.
0 commit comments