Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

JavaScript praktinė paskaita Nr 5

Antradienio paskaitoje nagrinėjome ir rašėme kaip galima sukurti kalkuliatorių naudojant masyvą mygtukams, tekstinį įvesties laukelį išveščiai ir vieną JavaScript funkciją atlikti skaičiavimams.

Žigsnis po žingsnio aiškinamės kodą.

Užduotys:

Visą kodą galite nusikopijuoti ir pasileisti tiesiai naršyklėje šiame puslapyje: https://codepen.io/Tarantulo/pen/rGdgaG

1. Mūsų kalkuliatoriui veikti bus reikalingti keli HTML elementai

Su body onload užkrauname funkciją. Įvestį id=”txt1″ naudosime išvesti skaičiams. Su id=”M_vieta” sugeneruosime mygtukus.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<body onload="kurtiMygtukus()">
<p><input type="text" id="txt1" value=""></p>
<div id="M_vieta" ></div>
<body onload="kurtiMygtukus()"> <p><input type="text" id="txt1" value=""></p> <div id="M_vieta" ></div>
<body onload="kurtiMygtukus()">
<p><input type="text" id="txt1" value=""></p>
<div id="M_vieta" ></div>

2. Sukuriamas masyvas, kuriame bus talpinami mūsų kalkuliatoriaus mygtukai.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var Mas1 = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "*", "/","C","=");
var Mas1 = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "*", "/","C","=");
var Mas1 = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "*", "/","C","=");

3. Sukuriame funkciją, kuri sugeneruos visus mūsų mygtukus.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function kurtiMygtukus() {
var temp_html = "";
for (x in Mas1) {
temp_html = temp_html + "<input type=button value=" + Mas1[x] + " onclick='rasom(\"" + Mas1[x] + "\"\)' </input>"
if ((x * 1 + 1) % 3 == 0) temp_html = temp_html + "<br>"
}
document.getElementById("M_vieta").innerHTML = temp_html;
}
function kurtiMygtukus() { var temp_html = ""; for (x in Mas1) { temp_html = temp_html + "<input type=button value=" + Mas1[x] + " onclick='rasom(\"" + Mas1[x] + "\"\)' </input>" if ((x * 1 + 1) % 3 == 0) temp_html = temp_html + "<br>" } document.getElementById("M_vieta").innerHTML = temp_html; }
function kurtiMygtukus() {
  var temp_html = "";
   
 for (x in Mas1) {
    temp_html = temp_html + "<input type=button value=" + Mas1[x] + " onclick='rasom(\"" + Mas1[x] + "\"\)' </input>"
   if ((x * 1 + 1) % 3 == 0) temp_html = temp_html + "<br>"
  }
  
  document.getElementById("M_vieta").innerHTML = temp_html; 
}

4. Sukuriame funkciją, kuri išves skaičius į txt1 laukelį ir atliks skaičavimus.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rasom(t1)
{
switch (t1) {
case "=":
document.getElementById("txt1").value = eval(document.getElementById("txt1").value); break;
case "C":
document.getElementById("txt1").value = ""; break;
default :
document.getElementById("txt1").value = document.getElementById("txt1").value + t1; break;
}
}
function rasom(t1) { switch (t1) { case "=": document.getElementById("txt1").value = eval(document.getElementById("txt1").value); break; case "C": document.getElementById("txt1").value = ""; break; default : document.getElementById("txt1").value = document.getElementById("txt1").value + t1; break; } }
function rasom(t1)
{
 switch (t1) {
     case "=":
  document.getElementById("txt1").value = eval(document.getElementById("txt1").value); break;
   case "C":  
       document.getElementById("txt1").value = ""; break;
     default :
 document.getElementById("txt1").value = document.getElementById("txt1").value + t1; break;
 }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *

Buy Me A Coffee
Thank you for visiting. You can now buy me a coffee!