// Программа поиска операторов мобильной связи Украины "use strict"; // Создаем объект с значениями ключей-массивами(коды операторов) var mobile = { Kyivstar: ["039", "067", "068", "096", "097", "098"], Vodafone: ["050", "066", "095", "099"], Lifecell: ["063", "093"], Intertelecom: ["094"], PEOPLEnet: ["092"], TriMob: ["091"] } // Запрашиваем у пользователя номер мобильного телефона var user = prompt("Введите номер мобильного телефона в формате: 000 1112233", ""); // Получаем доступ к первым 3-м индексам user var x = user.substr(0, 3); // Прогоняем в цикле все ключи for (var key in mobile) { var arr = mobile[key]; // Проходимся по всем массивам var check = arr.forEach(function (item, i, arr) { if (x === item) { //Выводим пользователю результат alert("Вас приветствует " + key); } }); } 

The task was this:

Create an object that contains the names and codes of mobile operators. The user enters a phone number and receives information about his operator.

I am not quite sure about the correctness of the solution to this problem.
It seems everything works, but is it right?

  • one
    No checks on the data entered: number of characters, numbers / not numbers. Also, the code does not report anything if the phone is entered, an operator that is not in the list. And so, everything works - Dmytryk
  • one
    And it is necessary to deliver a break if an operator is found, then there is no point in continuing the cycle. - Maxim Stepanov
  • one
    Regarding the verification, it may suit you, I did this on my website: I give the user to enter the phone as you please, without formats. Then I reformat the string with a regular expression, leaving only the numbers. And now, (if the numbers are 11, and the first is 7 or 8) - we take 3 digits starting from the 2nd. If 10 is just the first 3 digits. Well, if the numbers are not 10 or 11 - write the "wrong input". - Maxim Stepanov
  • one
    @VladSpirin Removed a comment, as it does not relate to this issue. Notified - read - deleted. - AK

1 answer 1

Inserted from your old check from the site, it may come in handy:

 // Создаем объект с значениями ключей-массивами(коды операторов) var mobile = { Kyivstar: ["039", "067", "068", "096", "097", "098"], Vodafone: ["050", "066", "095", "099"], Lifecell: ["063", "093"], Intertelecom: ["094"], PEOPLEnet: ["092"], TriMob: ["091"] } // Запрашиваем у пользователя номер мобильного телефона var user = prompt("Введите номер мобильного телефона", ""); var x; var y = -1; user = getTelNums(user); switch (user.length) { case 10: y = 0; break; case 11: y = 1; break; default: alert('неверный формат'); break; } if (y >= 0) { // Получаем доступ к первым 3-м индексам user var x = user.substr(y, 3); // Прогоняем в цикле все ключи var op = "Неизвестный оператор"; for (var key in mobile) { var arr = mobile[key]; // Проходимся по всем массивам var check = arr.forEach(function(item, i, arr) { if (x === item) { op = key; return; } }); } //Выводим пользователю результат alert('Вас приветствует '+op); } function getTelNums(telStr) { var regexpNums = /\d/g; var regexpTel = /^([7|8]{0,1}0\d{2})?\d{7}$/; var resultTel = ""; var checkNums = telStr.match(regexpNums); // из строки пользователя получаем массив цифр if (checkNums) { // если массив не пустой // сделаем из него строку: for (var j = 0; j < checkNums.length; j++) resultTel += checkNums[j]; // проверим, телефон ли это: if (!regexpTel.test(resultTel)) { resultTel = ""; } } return resultTel; }