Фильтрация диапазонов писем в Javascript - PullRequest
0 голосов
/ 30 сентября 2019

Мне нужно создать программу, которая принимает первую букву приглашения, и если эта буква находится между a и k, то она должна выдавать определенный вывод, если между l и p другая, и так далее. Есть ли способ сделать это, не записывая каждую букву алфавита вниз? (извините, я новый кодер)

1 Ответ

0 голосов
/ 30 сентября 2019

Я думаю, вам следует попытаться решить проблему, прежде чем спрашивать - чтобы вы могли показать, что вы уже пробовали.

Я думаю, что приведенный ниже фрагмент указывает вас в правильном направлении - но для этого требуется любой символ, а не только буквы. Вам нужно отфильтровать все, кроме строчных букв.

// UI elements
const input = document.getElementById('input1')
const result = document.getElementById('result')

// input event
// only the first character is taken into account
input.addEventListener('input', function(e) {
  // adding the characters of the input value to an array, and
  // picking the 0th element (or '', if there's no 0th element)
  const a = [...this.value][0] || ''
  let ret = ''

  if (a !== '') {
    // lowercaseing letters, so it's easier to categorize them
    ret = categorizeAlphabet(a.toLowerCase().charCodeAt(0))
  } else {
    ret = 'The input is empty'
  }

  // displaying the result
  result.textContent = ret
})

// you could use this function to filter and categorize
// according to the problem ahead of you - and return the result
// to be displayed.
// In this example this function is rather simple, but
// you can build a more complex return value.
const categorizeAlphabet = (chCode) => {
  return `This is the character code: ${chCode}`
}
<label>
  First character counts:
  <input type="text" id='input1'>
  </label>
<h3 id="result">The input is empty</h3>
...