Home > Blockchain >  How to tell to Javascript to compare the first element with the second and the second with the third
How to tell to Javascript to compare the first element with the second and the second with the third

Time:10-13

Hello guys I am trying to solve this problem with Javascript.

I want to "automate" my code for each element of my array. I am trying to figure out how I can loop into that array and make a comparison between element n1 with the element n2 and so on., this is my kind of pseudo-code:

is animal at position 0 === "sheep"? if not return 'Hey wolf go away!!' but if yes i want to start another comparison, is the animal to position 1 === to animal at position 0?

if yes return ' Hey sheep n0: Hello how are you?'
if not return 'Hey sheep n0:  you are going to be eaten!;
is the animal to position 1  === to animal at position 2? 
if yes 'sheep n1: hello how are you?'
if not return 'sheep n1 you are going to be eaten!'
and so on

I put in place my pseudo-code like this:

 function warnTheSheep(animal) {
  if (animal[0] === "wolf") {
    return "hey wolf go away!";
  } else if (animal[0] === "sheep" && animal[1] === "wolf") {
    return `hey sheep n(i) you are going to be eaten!! run`;
  } else {
    return "hey sheep n(i) hello how are you?";
  }
}

console.log(warnTheSheep(["sheep", "wolf", "wolf", "sheep"]));

Thank you for your support.

CodePudding user response:

From what I gathered from your question, a very simple recursive approach could look something like this:

function warnTheSheep(animal, index = 0) {
    if (index >= animal.length) {
        return;
    }

    if (animal[index] === "wolf") {
        console.log(`hey wolf n(${index}) go away!`);
    } else if (animal[index] === "sheep" && animal[index   1] === "wolf") {
        console.log(`hey sheep n(${index}) you are going to be eaten!! run`);
    } else {
        console.log(`hey sheep n(${index}) hello how are you?`);
    }

    warnTheSheep(animal, index   1);
}

warnTheSheep(["sheep", "wolf", "wolf", "sheep"]);

Your question was very unclear and some clarification would be appreciated in the future.

CodePudding user response:

This is how I would go about generating a useful array of warning messages.

The getWarnings function generates an array of statuses for each slot in the animals array. The warnTheSheep function just converts that to another array of nice human-readable messages.

const Status = {
  Danger: 0,
  Safe: 1,
  Wolf: 2
}

const Animal = {
  Wolf: "wolf",
  Sheep: "sheep"
}

function getWarnings(animals) {
  const result = [];
  for (let i = 0; i < animals.length; i  ) {
    const animal = animals[i];
    const nextAnimal = animals[i   1];
    if (animal === Animal.Wolf)
      result.push(Status.Wolf);
    else if (animal === Animal.Sheep && nextAnimal === Animal.Wolf)
      result.push(Status.Danger);
    else
      result.push(Status.Safe);
  }
  return result;
}

function warnTheSheep(animals) {
  const result = [];
  const warnings = getWarnings(animals);
  for (let i = 0; i < warnings.length; i  ) {
    const warning = warnings[i];
    if (warning === Status.Safe)
      result.push(`hey sheep ${i} hello how are you?`);
    else if (warning === Status.Danger)
      result.push(`hey sheep ${i} you are going to be eaten!! run`);
    else
      result.push(`hey wolf go away`);
  }
  return result;
}

console.log(warnTheSheep(["sheep", "wolf", "wolf", "sheep"]));

CodePudding user response:

I believe you can just loop through each element and compare it to the next element.

Could look something like this

for(let i = 0; i < animals.length; i  ) {
    if(animals[i 1]) { //Check if there is another element after the current one
        let animal = animals[i]; 
        let nextAnimal = animals[i 1]; //Next element in the array
        
        if (animal === "wolf") {
            return "hey wolf go away!";
        } else if (animal === "sheep" && nextAnimal === "wolf") {
            return `hey sheep n(${i}) you are going to be eaten!! run`;
        } else {
            return `hey sheep n(${i}) hello how are you?`;
        }
    }
}

Maybe this works

CodePudding user response:

if you wanted to automate it and compare your array with any index value you have to generate a random no.(int random_int = (int)Math.floor(Math.random()*(max-min 1) min)) and then pass the value into the function.now use this value as index no.

  • Related