0

There is any way to make a code that pick a random between these values:

let initialString = "single result:\nfoo||bar||anything||something"
let endString
setInterval(()=>{
    endString = // do some stuff do define its value
    console.log(endString)
},1000)
LuisF
  • 13
  • 4
  • 1
    Put the possibilities in an array. Then select a random array element and assign it to `endString`. – Barmar Jan 17 '23 at 17:41
  • What does this have to do with logical operators? They operate on true/false values, not strings and arrays. – Barmar Jan 17 '23 at 17:41
  • You can use the `split()` method to extract the words from `initialString`. Please show what you tried so we can help you fix it. – Barmar Jan 17 '23 at 17:44
  • See https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array for picking a random array element. – Barmar Jan 17 '23 at 17:45
  • Are you aware that `setInterval()` function works indefinitely until you stop it? I don't what you're trying to implement but it is not going in a good direction. – jkalandarov Jan 17 '23 at 17:59
  • @jkalandarov the initial ideia was to make it run indefinitely – LuisF Jan 19 '23 at 02:24
  • @Barmar the data comes in a string from a textarea tag, and i don't know if the possibilities will come at start, end or middle of the string, because of it i need to parse the string before of picking the parts that need to be replaced and doing it – LuisF Jan 19 '23 at 02:30
  • @Thank you for the tip to pick a random from array, as you can see in the answer i've used it – LuisF Jan 19 '23 at 02:32

2 Answers2

0

As @barmar suggested in his comment, You can simply achieve that by splitting a initialString into an array with the help of Array.split() method and then find the random element from that array and print it out.

Live Demo :

let initialString = "single result:\nfoo||bar||anything||something";

const arrString = initialString.split('||');

function getRandomItem(arr) {
    // get random index value
    const randomIndex = Math.floor(Math.random() * arr.length);
    // get random item
    const endString = arr[randomIndex];
    return endString;
}

console.log(getRandomItem(arrString));
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
0

I've found out that for my application is better to put the possibilities between parentheses to separate then from the parts of the string that don't need to be touched

let initialString = "single result:\n(foo|bar|anything|something)"
let endString
let stringChangeInterval = setInterval(() => {
    let validatedPairs = []
    for(let i = 0; i < initialString.length; i++){
        if(initialString[i] != "(") continue
        for(let j = i+1; j < initialString.length; j++){
            if(initialString[j] != ")") continue
            validatedPairs.push([i, j])
            i = j
            break
        }
    }
    endString = initialString
    for(let i = validatedPairs.length-1; i >= 0; i--){
        let possibilities =  initialString.substring(validatedPairs[i][0]+1, validatedPairs[i][1]).split('|')
        if(possibilities.length ===  1) continue
        let choosenPossibilty = randomFromArray(possibilities)
        endString = endString.replace(endString.substring(validatedPairs[i][0], validatedPairs[i][1]+1), choosenPossibilty)
    }
    console.log(endString)
}, 1000);

function randomFromArray(array){
    return array[Math.floor(Math.random() * array.length)]
}

EDIT:

The code above works fine if there is no repeated possibilities, otherwise the code will have a complete misbehavior, so I've made some adjustment to the code:

let initialString = "single result:\n(foo|bar|anything|something)"
let endString
let stringChangeInterval = setInterval(() => {
    let validatedPairs = []
    for(let i = 0; i < initialString.length; i++){
        if(initialString[i] != "(") continue
        for(let j = i+1; j < initialString.length; j++){
            if(initialString[j] != ")") continue
            validatedPairs.push([i, j])
            i = j
            break
        }
    }
    endString = initialString
    for(let i = validatedPairs.length-1; i >= 0; i--){
        let possibilities =  initialString.substring(validatedPairs[i][0]+1, validatedPairs[i][1]).split('|')
        if(possibilities.length ===  1) continue
        let choosenPossibilty = randomFromArray(possibilities)
        let lastIndex = endString.lastIndexOf(endString.substring(validatedPairs[i][0], validatedPairs[i][1]+1))
        endString = endString.substring(0, lastIndex)+choosenPossibilty+endString.substring(lastIndex + validatedPairs[i][1]+1 - validatedPairs[i][0])
    }
    console.log(endString)
}, 1000);

function randomFromArray(array){
    return array[Math.floor(Math.random() * array.length)]
}
LuisF
  • 13
  • 4