0

Как провести код через цикл, чтобы была возможность ввода три раза подряд после окончания первого раза?

var pName = prompt("Введите Продукт"),
  pManufacture = prompt("Введите Производителя"),
  pDate = prompt("Введите Дату"),
  pPrice = prompt("Введите Цену"),
  array = [],
  len,
  button = document.getElementById("button");

button.addEventListener("click", addUser, false);

function addUser() {
  array.push(createUser(pName, pManufacture, pDate, pPrice));
  len = array.length;
  array[len - 1].displayInfo();
}

function createUser(pName, pManufacture, pDate, pPrice) {
  return {
    name: pName,
    Manufacture: pManufacture,
    Date: pDate,
    Price: pPrice,
    displayInfo: function () {
      alert(
        "Продукт : " +
          array[len - 1].name[0].toUpperCase() +
          array[len - 1].name.slice(1) +
          " Производитель : " +
          array[len - 1].Manufacture[0].toUpperCase() +
          array[len - 1].Manufacture.slice(1) +
          " Дата : " +
          array[len - 1].Date +
          " Цена : " +
          array[len - 1].Price
      );
    }
  };
}
<!DOCTYPE html>
<html>
  <head>
    <title>123</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <button id='button'>тыкни</button>
  </body>
</html>

1 ответ 1

0

Сделайте просто инпуты, и вводите в них нужные данные.

Prompt не лучший вариант, и количество полей в него не добавить, если я правильно понял вопрос

const name = document.querySelector('#name')
const manufacture = document.querySelector('#manufacture')
const date = document.querySelector('#date')
const price = document.querySelector('#price')
const button = document.getElementById("button");

var result;

button.addEventListener("click", addUser, false);

function addUser() {
  result = createUser(name.value, manufacture.value, date.value, price.value);
  displayInfo(result);
}

function createUser(name, manufacture, date, price) {
  return {
    name: name,
    manufacture: manufacture,
    date: date,
    price: price,
  }
};

function displayInfo ({name, manufacture, date, price}) {
  alert(
    "Продукт : " +
    name.toUpperCase() +
    name.slice(1) +
    " Производитель : " +
    manufacture.toUpperCase() +
    manufacture.slice(1) +
    " Дата : " +
    date +
    " Цена : " +
    price
  );
}
<!DOCTYPE html>
<html>

<head>
  <title>123</title>
  <meta charset="utf-8" />
</head>

<body>
  <button id='button'>тыкни</button><br>
  <input id='name' placeholder='Введите Продукт' /><br>
  <input id='manufacture' placeholder='Введите Производителя' /><br>
  <input id='date' placeholder='Введите Дату' /><br>
  <input id='price' placeholder='Введите Цену' /><br>
</body>

</html>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.