Skip to content

Commit 54fc8c4

Browse files
committed
Fix: command output basic logic
1 parent 1035fd6 commit 54fc8c4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Hackerman/script.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
});

0 commit comments

Comments
 (0)