Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <!--
- DONE - externalizar o JS
- DONE - garantir que só depois de todo(s) o(s) doc(s) carregados é que solução poderá funcionar
- DONE - usar button (em vez de submit)
- -->
- <meta charset="UTF-8">
- <title>Numeração Romana</title>
- <!--
- O utilizador deverá indicar uma fronteira de inteiros;
- por exemplo, entre 1 e 33,
- e o sistema deverá mostrar-lhe a escrita desses
- inteiros, não na notação decimal do dia-a-dia,
- mas antes na notação "romana".
- -->
- <script src="4.js"></script>
- </head>
- <body>
- <!-- ol com type i, numera os seus list items
- com numeração romana (em letra pequena)
- -->
- <!--
- <ol type="i">
- <li>Max Verstappen</li>
- <li>Sergio Verstappen</li>
- </ol>
- -->
- <hr>
- <!--
- sem submit, a action será ignorada
- -->
- <form
- action="javascript:obterEscritaEmNotacaoRomana();"
- >
- <fieldset>
- <legend lang="pt">Fronteira de valores</legend>
- <!-- primeiro valor -->
- <label for="idNumInicial">Valor inicial:</label>
- <input
- type="number"
- min="1"
- max="9999"
- value="1"
- placeholder="1"
- id="idNumInicial"
- name="nameNumInicial"
- >
- <br>
- <!-- último valor -->
- <label for="idNumFinal">Valor final:</label>
- <input
- type="number"
- min="1"
- max="9999"
- value="33"
- placeholder="33"
- id="idNumFinal"
- name="nameNumFinal"
- >
- </fieldset>
- <!-- elemento para desencadear o pedido, a computação -->
- <!--
- <input
- type="submit"
- value="ver este intervalo de números em notação romana"
- >
- -->
- <!-- TODO: operacionalizar o button -->
- <input
- id="idBtnSubmeter"
- type="button"
- value="ver este intervalo de números em notação romana"
- >
- </form>
- <hr><!-- quebra semântica -->
- <section id="idSectionFeedback">Aqui aparecerão os resultados.</section>
- </body>
- </html>
- ****
- //nomeDoObjeto.onNomeDoEvento=respostaAoEvento
- //nomeDoObjeto.onNomeDoEvento=nomeDeFunctionAExecutarParaResponder
- window.onload=boot;
- //constantes com os ids dos elementos HTML para os quais queremos "ponte"
- const ID_NUM_INICIAL = "idNumInicial"
- const ID_NUM_FINAL = "idNumFinal"
- const ID_SECTION_FEEDBACK = "idSectionFeedback"
- const ID_BTN_SUBMETER = "idBtnSubmeter"
- //variáveis de nível módulo, acessíveis a qq function
- //para corresponderem aos elementos HTML deste Sistema Web
- var oNumInicial,
- oNumFinal,
- oSectionFeedback,
- oBtnSubmeter;
- /*
- boot será invocado AUTOMATICAMENTE pelo browser (window)
- quando o próprio detetar a ocorrência do evento "load"
- que acontece quando TODO o HTML e TODO o JS tiverem
- sido carregados em memória.
- A chamada a boot acontecerá exatamente 1 vez.
- */
- function boot(){
- //bridging html to javascript
- //1 - associações entre vars JS e elementos HTML
- oNumInicial = document.getElementById(ID_NUM_INICIAL)
- oNumFinal = document.getElementById(ID_NUM_FINAL)
- oSectionFeedback = document.getElementById(ID_SECTION_FEEDBACK)
- oBtnSubmeter = document.getElementById(ID_BTN_SUBMETER)
- //coisa = document.getElementById("NAO_EXISTE") //Null
- //coisa.onclick = fazerAlgo; //ERROR!!!!!!!
- //var RELEVANTES = new Array();
- var coisaInexistente=document.getElementById("idXpto");
- var RELEVANTES = [
- oNumInicial, oNumFinal, oSectionFeedback, oBtnSubmeter
- //,coisaInexistente
- ]
- bOK = qualityControl(RELEVANTES);
- if (!bOK){ //if not ok
- window.alert("Critical Error. System unavailable.")
- return;
- }
- //2 - conferir comportamentos
- //nomeDoObjeto.onevento=eventoHandler
- //notar q NÃO se está a fazer uma chamada
- //está a indicar-se o nome de uma function a chamar
- //quando (e se) o evento de "click" ocorrer.
- //Uma função q se atribua a um evento chama-se
- //um "event handler"
- oBtnSubmeter.onclick=obterEscritaEmNotacaoRomana;
- oNumInicial.onchange = oNumFinal.onchange = reagirMudancaDeIntervalo;
- //cascade assignment
- //oBtnSubmeter.onclick = oNumInicial.onclick = oNumFinal.onclick = obterEscritaEmNotacaoRomana;
- }//boot
- //event handler!
- function reagirMudancaDeIntervalo(){
- var iNumInicial = Number(oNumInicial.value);
- var iNumFinal = Number(oNumFinal.value);
- var bProblema = iNumInicial>iNumFinal;
- if (bProblema){
- var strResposta = "<mark>Pf, escreva um número final superior ao inicial.</mark>";
- oSectionFeedback.innerHTML = strResposta;
- }
- else{
- obterEscritaEmNotacaoRomana();
- }
- }//reagirMudancaDeIntervalo
- //outro event handler
- function obterEscritaEmNotacaoRomana(){
- var iNumInicial, iNumFinal;
- iNumInicial = Number(oNumInicial.value); //type casting de string para Number
- iNumFinal = Number(oNumFinal.value); //type casting de string para Number
- strHtmlResposta = "<ol type=\"i\" start=\""+iNumInicial+"\">";
- for
- (
- var iNumDoMomento=iNumInicial; //zona de init
- iNumDoMomento<=iNumFinal; //zona de continuidade check
- iNumDoMomento+=1 //zona de atualização
- ){
- //corpo
- var strNovaLinha="<li>";
- strNovaLinha+=(iNumDoMomento+""); //notar: a conversão explícita de Number para string
- strNovaLinha+="</li>"
- strHtmlResposta+=strNovaLinha
- }//for
- strHtmlResposta += "</ol>"; //+= concatenação cumulativa
- oSectionFeedback.innerHTML=strHtmlResposta;
- }//obterEscritaEmNotacaoRomana
- /*
- var aluno = {
- "nome":"Artur",
- "num":4321,
- "classificao":0
- }
- for (var propriedade in aluno){
- document.write(propriedade)
- }
- */
- function qualityControl(pColCriticalObjects){
- /*
- for (var criticalObject of pColCriticalObjects){
- var bOK = criticalObject!=null;
- if (!bOK){
- return false;
- }
- }//for
- */
- for (var idx=0; idx<pColCriticalObjects.length; idx+=1){
- var currentElement = pColCriticalObjects[idx]
- var bOK = currentElement!=null;
- if(!bOK){
- return false;
- }
- }//for
- return true;
- }//qualityControl
Advertisement
Add Comment
Please, Sign In to add comment