0

I had this this function and what it does to fetch value from network response and I manage to display the output 0: {recipeName: value_1} and 1: {recipeName: value_2}. But now the problem I want to display the result together with an alert message. Any idea how to make alert message display only once but the result display in loop?

function (e) {
  var returnedData = JSON.parse(e.responseText);
  console.log(returnedData);

  if(returnedData.length != 0){
    for (var x=0; x < returnedData.length; x++ ){
      var listOfRecipe = '('+[x]+') ' + returnedData[x]['recipeName'];
    }
    alert("Recipe cannot be deactivated, it is used in the following sub recipes: " + listOfRecipe);
  }
}

Current output

Recipe cannot be deactivated, it is used in the following sub recipes: (1) value_2

What I want

Recipe cannot be deactivated, it is used in the following sub recipes: (0) value_1 (1) value_2

4
  • what is the current content of your alert now ? Commented Jan 23, 2020 at 3:03
  • Recipe cannot be deactivated, it is used in the following sub recipes: (1) value_2 Commented Jan 23, 2020 at 3:05
  • i mean in your current work, what is the result on that ? seems it's already working in our code Commented Jan 23, 2020 at 3:06
  • Did you mean keep this alert active while updating its content constantly? Commented Jan 23, 2020 at 3:06

1 Answer 1

2

Decalare your listofRecipe outside of your loop. Then change your code into this

function (e) {
  var returnedData = JSON.parse(e.responseText);
  var listOfRecipe  = ''
  console.log(returnedData);

  if(returnedData.length != 0){
    for (var x=0; x < returnedData.length; x++ ){
       listOfRecipe += '('+[x]+') ' + returnedData[x].recipeName;
    }
    alert("Recipe cannot be deactivated, it is used in the following sub recipes: " + listOfRecipe);
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I dont think the outcome will change. Still getting the same result. I still getting the last result which is value_2
I upadted my answer
forgive me.. I miss the += sign

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.