Advertisement
irmantas_radavicius

Untitled

Dec 5th, 2021
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <title>Javascript</title>  
  5.     <style>
  6.     </style>
  7. </head>
  8. <body> 
  9.     <div id="div1">Hello world</div>
  10.     <div id="div2">Hello oh my</div>
  11.     <div id="div3">Hello very hello</div>
  12.    
  13.     <script>       
  14.         function sum(a, b){
  15.             // input + validation          
  16.             let checkA = ((typeof(a) === "number") && (isFinite(a)));
  17.             let checkB = ((typeof(b) === "number") && (isFinite(b)));
  18.             if (!checkA || !checkB){
  19.                 return undefined;  
  20.             }                  
  21.             // algorithm = logic           
  22.             let sum = a + b;
  23.            
  24.             // output              
  25.             return sum;
  26.         }
  27.        
  28.         function setStyle(id, prop, value){
  29.             // Užduotis: modifikuoti galima tik funkcijos setStyle viduje esantį kodą; kodo už šios funkcijos ribų liesti negalima, įskaitant testus žemiau. Tikslas - padaryti kad konsolėje nėra "raudonų klaidų".       
  30.        
  31.             // input
  32.             let checkId = typeof (id) == "string";
  33.             let checkProp = typeof (prop) == "string";
  34.             let checkValue = typeof (value) == "string";
  35.            
  36.             if(!checkId || !checkProp || !checkValue)
  37.                 return undefined;
  38.            
  39.             // algorithm
  40.             let elem = document.getElementById(id);
  41.             if (elem != null){
  42.                 elem.style[prop] = value;          
  43.             }          
  44.             // output      
  45.             return (elem != null) ? elem.style : null;         
  46.            
  47.             // Nuo šitos vietos kodo modifikuoti neleidžiama
  48.         }
  49.                
  50.         setStyle("div", "color", "orange");
  51.         setStyle("div1", "color", "red");
  52.        
  53.         // Reikia padaryti, kad žemiau esančios eilutės veiktų
  54.         setStyle("div2").color = "green";
  55.         setStyle("div3").color = "blue";       
  56.        
  57.         // Problema - funkciją turi būti galima naudoti ir nenurodžius antro (prop) bei trečio (value) parametrų; dabar funkcija neleidžia kad jie būtų undefined. Reikia leisti, ir padaryti, kad jei jie undefined - 42 eilutė nevykdoma, o funkcija tiesiog grąžina stilių (45 eil.), kurį vėliau galima naudoti (žr. 54-55 eil.). Dabar jos neveikia, nes funkcija grąžina undefined, ir negalima sakyti undefined.color = ...
  58.     </script>      
  59. </body>
  60. </html>
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement