Помогите с кодом js
У меня есть такой код:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
let chatReturn = '';
function initialize(user) {
console.log('==============================');
console.log(`Hello, ${user}; Using made JS OS`);
console.log('Type help to show command list');
console.log('==============================');
}
function lt(local, num) {
let i = num;
let array = [];
while (local.charAt(i)!==' ') {
array.push(local.charAt(i));
i++;
}
return array.join('');
}
function cmd() {
chatReturn = prompt('>');
if (chatReturn=='help') {
console.log('help => show this');
console.log('create [filename] => create new file');
console.log('edit [filename] => edit created file');
console.log('math [figure 1] [figure 2] [operator] => return math operation');
console.log('var [variable] => creates new variable');
console.log('set [variable] => edit variable value');
console.log('time => show time since the launch');
console.log('import => imports library from source code');
console.log('run [filename] => starts-up js code file');
console.log('log [message] => outputs message to console');
console.log('alc [var1 ] [var 2] [operator] => return variable math operation');
console.log('rnd => outputs random number (within 1) to console');
console.log('clr => clears console');
}
if (lt(chatReturn)=='create') {
}
if (lt(chatReturn)=='edit') {
}
if (lt(chatReturn)=='math') {
}
if (lt(chatReturn, 1)=='var') {
console.log(chatReturn)
console.log(lt(chatReturn, 0))
}
if (lt(chatReturn)=='set') {
}
if (lt(chatReturn)=='time') {
}
if (lt(chatReturn)=='import') {
}
if (lt(chatReturn)=='run') {
}
if (lt(chatReturn)=='log') {
}
if (lt(chatReturn)=='alc') {
}
if (chatReturn=='rnd') {
console.log('wfg');
}
if (lt(chatReturn)=='clr') {
}
}
function update() {
while (true) {
cmd();
}
}
initialize('TestUser');
update();
я выделил для теста команду var. Когда ввожу ее в консоль ничего не выводится и node js пишет что сессия закончилась. Отдельно функция lt() работает хорошо (она должна отделять слова) возможно ошибка в компиляторе я не знаю. Простите за кучу ифов но я потом оптимизирую код. Так в чем проблема: надо чтобы функция lt() отделяла аргументы команды но она не выводит ничего, а после команды help символ `>` не выводится в консоль и я не могу вводить команды под функцией cmd()
По дате
По рейтингу
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
let chatReturn = '';
function initialize(user) {
console.log('==============================');
console.log(`Hello, ${user}; Using made JS OS`);
console.log('Type help to show command list');
console.log('==============================');
}
function cmd() {
chatReturn = prompt('>');
const args = chatReturn.split(' ');
const command = args[0];
switch (command) {
case 'help':
showHelp();
break;
case 'var':
handleVar(args);
break;
// Add more cases for other commands
default:
console.log('Unknown command. Type "help" to see the list of available commands.');
}
}
function showHelp() {
console.log('help => show this');
console.log('create [filename] => create new file');
console.log('edit [filename] => edit created file');
console.log('math [figure 1] [figure 2] [operator] => return math operation');
console.log('var [variable] => creates new variable');
console.log('set [variable] => edit variable value');
console.log('time => show time since the launch');
console.log('import => imports library from source code');
console.log('run [filename] => starts-up js code file');
console.log('log [message] => outputs message to console');
console.log('alc [var1 ] [var 2] [operator] => return variable math operation');
console.log('rnd => outputs random number (within 1) to console');
console.log('clr => clears console');
}
function handleVar(args) {
if (args.length < 2) {
console.log('Usage: var [variable]');
return;
}
console.log(`Creating variable: ${args[1]}`);
// Add logic to create/set the variable
}
function update() {
while (true) {
cmd();
}
}
initialize('TestUser');
update();
Больше по теме