Using user input to create conditional statements

I'm currently working on a project involving a textbot that outputs information in the console based on user input. It sounds simple, but I've encountered a frustrating problem that I can't seem to solve. There must be an easy fix for this issue, but it's eluding me.

My goal is to have the textbox recognize a command that includes both the user input and a variable provided by the user. Here is an example of what I'm aiming for:

case "input " + condition {
  //execute code based on 'condition'
}

My inquiry is how can I differentiate between the user input and the specified condition within the textbox? It may sound confusing, but I'm struggling to frame it differently.

Edit: Here's an illustration for clarity:

Textbox input:

"!locate p.actionValue"

Depending on the value assigned to 'actionvalue' by the user, the response should vary like so:

if (actionvalue = 32) {
  //perform an action when actionvalue equals 32
}

The challenge lies in distinguishing between the command and the condition. Any advice or suggestions would be greatly appreciated.

Answer №1

To extract specific information from the input, you can utilize a regular expression and check for certain conditions:

let match;
if (match = input.match(/^!find (.*)/)) {
    let condition = match[1];
    // perform actions based on the condition
}

To make the code more versatile, consider a broader approach:

let match = input.match(/^!(\S+)\s+(.*)/);
if (match) {
    let action = match[1];
    let argument = match[2];
    switch (action) {
        case 'find':
            ...
            break;
    }
}

Answer №2

If you want to extract commands from your text, one way is to split the text using "input " as the delimiter.

// Splitting the text by 'input '
var extractedCommand = inputText.split('input ');
// The desired command can be accessed using extractedCommand[0]

It's important to ensure that the text actually contains 'input ', and consider scenarios like 'input input '.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having issues with the functionality of Bootstrap 4 popover?

In my Spring MVC web project, a Bootstrap popover appears when the help icon is clicked. However, on the first click, the popover opens and moves away from the icon. After closing it and clicking again, the popover correctly positions itself. When I chan ...

Why isn't the dropdownlist setting a default value in Javascript?

<select id="modalAdd_DDLSample"> </select> <option value=0> PC</option> <option value=1> MAC </option> <option value=2> Rasberry </option> $("#modalAdd_DDLSample").val("1"); $("#modalAdd_DDLSample").val(1); ...

What is preventing me from binding the array index to a model in AngularJS with two-way binding?

I'm currently facing a challenge with a seemingly simple task: I have an array of string values that I want to link to a select element's options, and then connect the index of the chosen value to a variable. However, I have found that neither us ...

Create a function that adds a new div element with a one-of-a-kind identification when

I am currently developing a web application on www.firstusadata.com/cash_flow_test/. The functionality involves two buttons that add products and vendors dynamically. However, the issue I'm facing is that the appended divs do not have unique IDs, maki ...

What is the best way to implement dynamic generation of Form::date() in Laravel 8?

Is there a way to dynamically generate the Form::date() based on the selection of 1? If 1 is selected, then display the Form::date() under the Form::select(). However, if 0 is selected, then hide the Form::date() in this particular view. For example: Sel ...

What are the steps to launching a yarn app on a dev server?

I've always stuck with npm and never ventured into using yarn or webpack explicitly. The code from this repository needs to be executed: https://github.com/looker-open-source/custom_visualizations_v2 I'm looking for a way to run it like a develo ...

The Final Div Fluttering in the Midst of a jQuery Hover Effect

Check out my code snippet here: http://jsfiddle.net/ZspZT/ The issue I'm facing is that the fourth div block in my example is flickering quite noticeably, especially when hovering over it. The problem also occurs occasionally with the other divs. Ap ...

Conditionally submit a form using JQuery

I need to implement a validation condition using jQuery for a form submit button. The requirement is that the form should only be submitted if the user enters a valid email address. HTML code : <form method="post" action="{% url sportdub.views.login ...

Issue with resetting Knockout.JS array - unable to make it work

Hey everyone, check out the project I'm currently working on over at Github: https://github.com/joelt11753/Udacity-map In this project, I have a menu generated from a list that can be filtered using an HTML select element. Initially, all items are di ...

If the session cannot be located, users will be redirected to the sign-in page

I have a small application that utilizes next-auth to display a signin/signout button based on the user's sign-in status. The buttons function correctly and redirect me to the signin page when clicked. However, I am wondering how can I automatically ...

Having trouble with transferring JSON data as a string from POSTMAN to a node server

My JSON data structure is as follows: const json = { "name": "Peter", "age": 21 } After calling JSON.stringify(json), the result is: '{"name":"Peter","age":21}' I am currently us ...

How can I use VueJS and Vue Router to generate a details page for a list item?

I am currently working with a list of items sourced from a JSON file and looping through them. However, I want to create individual details pages for each item using VueRouter. Any advice on how I can accomplish this? I am facing difficulties in passing t ...

Using the ng-src and ng-repeat directives in combination within an if/else statement

Seeking assistance with implementing an if/else statement to display different pictures based on a condition in AngularJS. Here is an example of the controller code: $scope.getFeatureNo = function (thisCase) { if (thisCase.featureno == 1) { ...

The error message that you are seeing is indicating that the `contracts` property of `this.state

Despite having encountered this issue before, I am still struggling to find a solution. The code snippet for fetching data is as follows: async componentDidMount(){ try { const res = await fetch('https://example.com/data.json'); ...

Tips for creating a cohesive group of HTML elements within an editable area

Imagine having a contenteditable area with some existing content: <div contenteditable="true"> <p>first paragraph</p> <p> <img width='63' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-s ...

What is the reason behind the length property belonging to an array object?

While there are other instances, let's focus on the length property for now. Why does it appear as if we're copying it here: [].hasOwnProperty("length") //==> true It is common knowledge that an array's length property belongs ...

Tips for preventing the occurrence of numerous instances of braintree.setup in Angular

I am encountering an issue with a Braintree payment form displayed in a modal window: $scope.displayModalBraintree = function () { $scope.modal = 'modal_payment_form.html', $scope.$on('$includeContentLoaded', function () { ...

Is it possible for me to create a hyperlink that directs to a javascript function?

Here is the code I am currently using: <input class="button" type="button" value="Mark" onClick="doCheck('mark');" \> However, I would like to replace the button with a hyperlink using the <a> tag. Is it possible to achieve ...

When utilizing the vue @submit directive, the FormData object may be found to be devoid

Encountering a unique edge case, I found a solution. When creating a form with Vue, I noticed that empty strings were being sent to the server. Upon investigation, it became apparent that the issue lies within the @submit directive. Interestingly, when uti ...

Top method for integrating configuration into a module

I'm trying to create a module called something.js that depends on a configuration, but I don't want the module itself to explicitly require the config. Additionally, I need my code editor to be able to analyze the module and provide autocomplete ...