Skip to content

Instantly share code, notes, and snippets.

@gabizinha12
Created July 18, 2020 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabizinha12/6489cb641f1206b2723842b7bf6246f7 to your computer and use it in GitHub Desktop.
Save gabizinha12/6489cb641f1206b2723842b7bf6246f7 to your computer and use it in GitHub Desktop.
let num = document.querySelector('input#fnum')
let lista = document.querySelector('select#flista')
let res = document.querySelector('div#res')
let valores = []
function isNumero(n) {
if (Number(n) >= 1 && Number(n) <= 100) {
return true
} else {
return false
}
}
function inLista(n, l) {
if (l.indexOf(Number(n) != -1)) {
return true
} else {
return false
}
}
function adicionar() {
if (isNumero(num.value) && !inLista(num.value, valores)) {
valores.push(Number(num.value))
let item = document.createElement('option')
item.text = `Valor ${num.value} adicionado.`
lista.appendChild(item)
} else {
window.alert('Valor inválido ou já encontrado na lista')
}
num.value = ''
num.focus()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment