S
S
SpideR-KOSS2018-09-19 16:33:54
JavaScript
SpideR-KOSS, 2018-09-19 16:33:54

What is the correct way to use a for loop?

Tell me about the for loop.
There are variables that store data from the form.

const name = req.body.name;
const email = req.body.email;

There is an array with validation error objects.
const arr = errors.array();
The content of the array.
[ { location: 'body',
    param: 'name',
    value: '',
    msg: 'Введите свое имя!' },
  { location: 'body',
    param: 'email',
    value: '',
    msg: 'Проверьте правильность E-mail!' }
  ]

There are two functions.
function nameField() {
      for (const namearr of arr.values()) {
        if (namearr.param !== "name") {
          return name;
        } else {
          console.log("Такое имя существует!");
        }
      }
    }

    function emailField() {
      for (const emailarr of arr.values()) {
        if (emailarr.param !== "email") {
          return email;
        } else {
          return console.log("Такой email существует!");
        }
      }
    }

If the array contains the string "name", return the variable name, if there is "email", return the variable email.
One function works, but not together, only one function works, the one that was launched first.
Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergski, 2018-09-19
@sergski

why two functions? So

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question