JavaScript Evaluate dynamically user-added If-Statements

How can I achieve that users can add multiply custom if-statements?

For example let's say there is a given variable called x with a given value of let's say 8. The user sees that x = 8 and has a button to add an if-statement. He clicks the button and can insert the condition which triggers an event (let's say it prints "Hello World"). So he enters "x < 100" into the field which is true. Therefore "Hello World" is printed. After clicking the button once again, he is able to add an other condition, let's say "x < 7" which is also true. Because both conditions are true, "Hello World" is still printed. I think you got the point of my questions, even though I lack the vocabulary. So how could I manage to let user add an undefined amount of conditions which will be checked before "Hello World" is printed? The only solution I know is to limit the possible amount of conditions and check each one if it is empty / what the conditions says.

Thanks a lot!

Answer:1

First you need a way to switch between operators. A very simple enum is perfect for this. Just add all the operators you want to use.

enum Operator : String {

    case biggerThan = ">"
    case smallerThan = "<"
    case equal = "=="

    init?(string:String) {
        switch string {
        case ">" :
            self = .biggerThan
        case "<" :
            self = .smallerThan
        case "==" :
            self = .equal
        default :
            return nil
        }
    }
}

Each time a user clicks a button and inserts a condition, a corresponding Condition value will be created.

struct Condition {
    var value: Int
    var operation: Operator
}

This function returns a Bool depending on x, the inputValue and the chosen operator.

func checkCondition(x: Int, condition: Condition) -> Bool {
    switch condition.operation {
    case .biggerThan :
        return condition.value > x
    case .smallerThan :
        return condition.value < x
    case .equal :
        return condition.value == x
    }
}

This does the same but for a whole bunch of conditions. Here you can implement more logic. If all need to be true for example add : if !result { return false }.

func checkAllConditions(x:Int, conditions: [Condition]) {
    for condition in conditions {
        let result = checkCondition(x, condition: condition)
        print(result)
    }
}

Now all you need to do is store conditions in an array as the user creates them

func userCondition(operation:String, input:String) -> Condition? {

    guard let op = Operator(string: operation) else {
        return nil
    }
    guard let doubleValue = Double(input) else {
        return nil
    }

    return Condition(value: Int(doubleValue), operation: op)
}

let conditionA = userCondition("<", input: "10")! // use if let instead of !
let conditionB = userCondition(">", input: "10")! // use if let instead of !
let conditionC = userCondition("==", input: "23")! // use if let instead of !

var x : Int = 23

checkAllConditions(x, conditions: [conditionA,conditionB,conditionC])
Answer:2

I am writing a chrome extension to replace all the occurrences of certain set of image ( I have the image names) and replace it with another image (src) I need to find all the occurrences of a ...

I am writing a chrome extension to replace all the occurrences of certain set of image ( I have the image names) and replace it with another image (src) I need to find all the occurrences of a ...

  1. find image name in word
  2. find image name on google
  3. find image name in powerpoint 2016
  4. find image name
  5. find image name in powerpoint
  6. find image name in pdf
  7. find image name on iphone
  8. find image name jquery
  9. find docker image name
  10. tasklist find image name
  11. azure find image name
  12. dism find image name
  13. find image file name
  14. xcode find image name
  15. upload image find name
  16. find facebook from image name
  17. how to find image name in excel
  18. find image to font name
  19. find your name image
  20. unity find image by name

Hoping one of you guys can help me here. I'm trying to import data into excel from this webpage as well as some others that are the same website: https://re.po.st/qZZYQJ - basically I want to take ...

Hoping one of you guys can help me here. I'm trying to import data into excel from this webpage as well as some others that are the same website: https://re.po.st/qZZYQJ - basically I want to take ...

  1. scrape html page python
  2. scrape html page php
  3. scrape html page javascript
  4. scrape html page
  5. scrape html page c#
  6. java scrape html page
  7. scrapy scrape html page
  8. html page scrape example

I am new to javascript (new to programming overall, really). And I encountered this behavior of for/in loop that I don't quite understand. The following pieces of code were run with $node command in ...

I am new to javascript (new to programming overall, really). And I encountered this behavior of for/in loop that I don't quite understand. The following pieces of code were run with $node command in ...

  1. javascript object assignment by value or reference
  2. javascript object assignment operator
  3. javascript object assignment shorthand
  4. javascript object assignment by value
  5. javascript object assignment property
  6. js object assignment
  7. javascript object destructuring assignment
  8. javascript object variable assignment
  9. javascript object key assignment
  10. javascript object literal assignment
  11. javascript es6 object assignment
  12. javascript default object assignment
  13. js object destructuring assignment

I'm running into some frustrating 404 errors while working on a web project. I'm using this file structure for my CSS and JS files: bower_components bootstrap |dist |css ...

I'm running into some frustrating 404 errors while working on a web project. I'm using this file structure for my CSS and JS files: bower_components bootstrap |dist |css ...

  1. failed load resource
  2. failed load resource dll teamviewer
  3. failed to load resource 404
  4. failed to load resource the server responded with a status
  5. failed to load resource 500
  6. failed to load resource favicon.ico