1

I am new to working in js so that my code is not working

my code is:

let output = document.getElementById("output");
let addfruit;
let fruitList = [];
let howManyTimes = prompt("how many fruit to want to add?");
for (let index = 0; index < howManyTimes.length; index++) {
       addfruit = prompt("enter fruits name");
    if (addfruit == "no") {
        break;
    }
  fruitList.push(addfruit);
}
for (myShop of fruitList) {
    output.innerHTML += myShop + "<br/>";
}
 

my problem is that there is when I want 5 times prompt then I will get only 1 prompt

any help thanks.

7
  • Javascript is asynchronous so loop will not wait for prompt to close Commented Jun 30, 2020 at 5:34
  • you will consider howManyTimes as array but it is variable Commented Jun 30, 2020 at 5:34
  • 1
    @Ajith Agree with you. You can read more about async JS here: developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous Commented Jun 30, 2020 at 5:35
  • 2
    @Ajith: That's not true. Of course the loop will wait. If a prompt was async, it wouldn't be very useful. Commented Jun 30, 2020 at 5:53
  • @AlwaysHelping: Nothing in the code above will run asynchronously; the info in that link won't apply here. Commented Jun 30, 2020 at 5:56

4 Answers 4

2

howManyTimes is a string, not an array.

You need to convert it to number and then use it in loop condition

let howManyTimes = Number(prompt("how many fruit to want to add?"));

You can also rely on javascript's type coercion to convert howManyTimes to a number. So you can skip explicitly converting howManyTimes to a number.

for (let index = 0; index < howManyTimes; index++) {
    // code
}
Sign up to request clarification or add additional context in comments.

Comments

1

You do not have use .length .length can be used for arrays

Do something like that

Run Snippet to see it work.

let output = document.getElementById("output");
let addfruit;
let fruitList = [];
let howManyTimes = prompt("how many fruit to want to add?");
for (let index = 0; index < howManyTimes; index++) {
  addfruit = prompt("enter fruits name");
  if (addfruit == "no") {
    break;
  }
  fruitList.push(addfruit);
}
for (myShop of fruitList) {
  output.innerHTML += myShop + "<br/>";
}
<div id="output"></div>

Comments

0

You can be used that.

let output = document.getElementById("output");
let addfruit;
let fruitList = [];
let howManyTimes = prompt("how many fruit to want to add?");
for (let index = 0; index < howManyTimes; index++) {

    addfruit = prompt("enter fruits name");
    if (addfruit == "no") {
        break;
    }
    fruitList.push(addfruit);
}
for (myShop of fruitList) {
    output.innerHTML += myShop + "<br/>";
}

you will run this

Comments

0

Just removing '.length' property in the input howManyTimes works,

let output = document.getElementById("output");
let addfruit;
let fruitList = [];
let howManyTimes = prompt("how many fruit to want to add?");
for (let index = 0; index < howManyTimes; index++) {
       addfruit = prompt("enter fruits name");
    if (addfruit == "no") {
        break;
    }
  fruitList.push(addfruit); 
   
}
for (myShop of fruitList) {
    output.innerHTML += myShop + "<br/>";
}

Comments

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.