» »

Javascript

Javascript

Ales47 ::

Programiral sem nek program oz preko jave in mi ne gre izracunati vsote. Pri vsoti mi napise "Vsota: NaN".
Ne najdem napake oz ne ugotovim , kaj je narobe.

Koda:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" type="text/css" href="slog.css" />
  <title></title>
</head>
<body>

<script type="text/javascript">

var x = window.prompt("Vnesi število a: ");
var y = window.prompt("Vnesi število b: ");
a=isNaN(x);
b=isNaN(y);

if(a==false && b==false)
{
    if(x>y)
    {
      document.write("Prvo število je večje <br/>");
      document.write("a: ");
      document.write("<div id='vecje'>");
      document.write(x);
      document.write("</div>");
      document.write("b: ");
      document.write("<div id='manjse'>");
      document.write(y);
      document.write("</div>");
    
    }
      else
      {
	document.write("Drugo število je večje <br/>");
	document.write("a: ");
	document.write("<div id='vecje'>");
	document.write(y);
	document.write("</div>");
	document.write("b: ");
	document.write("<div id='manjse'>");
	document.write(x);
	document.write("</div>");
    
      }

   if(b==0)
  {
  document.write("<br/>");
  document.write("Produkt: ");
  document.write(x*y);
  document.write("<br/>");
  document.write("Vsota: ");
  document.write(parseInt("x",y));
  document.write("<br/>");
  document.write("Razlika: ");
  document.write(x-y);
  document.write("<br/>");
  document.write("Ostanek pri deljenju: ");
  document.write("<div id='manjse'>");
	document.write("Ne morem ga izračunate");
	document.write("</div>");
  document.write("Koeficient: ");
 document.write("<div id='manjse'>");
	document.write("Ne morem ga izračunate");
	document.write("</div>");
}

  else
{
  document.write("<br/>");
  document.write("Produkt: ");
  document.write(x*y);
  document.write("<br/>");
  document.write("Koeficient: ");
  document.write(x/y);
  document.write("<br/>");
  document.write("Vsota: ");
  document.write(x+y);
  document.write("<br/>");
  document.write("Razlika: ");
  document.write(x-y);
  document.write("<br/>");
  document.write("Ostanek pri deljenju: ");
  document.write(x%y);
  document.write("<br/>");
}
 }

else
{
    document.write("<div id='manjse'>");
    document.write("Ena od vnesenih vrednosti ni število");
    document.write("</div>");
}
</script>

</body>
</html>


Program gleda takole:

Prvo število je večje
a:
5
b:
2

Produkt: 10
Vsota: NaN
Razlika: 3
Ostanek pri deljenju:
Ne morem ga izračunate
Koeficient:
Ne morem ga izračunate

AndyS ::

Vrstica 53: x+y

AndyS ::

Verjetno ti še kaj drugega ni jasnega:
V vrsticah 14 in 15, a-ju in b-ju določiš boolean, torej true false. ker sta oba x in y cifri, dobita oba vrednost false. V 46ti vrstici se 0 enači s false, in prideš v prvi del vejitvenega stavka. Tako prideš v vrstico 53, kjer imapš prej opisano napako

amacar ::

Evo smo imeli podoben program za napisati v 1. semestru (tu še obarva z rdečo/zeleno manjšo/večjo številko, itd)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Pozdrav</title>
<style type="text/css">
	span.pozitivno {color:green;font-weight:bold}
	span.negativno {color:red;font-weight:bold}
</style>
<script type="text/javascript">
var prvo=parseInt(prompt("Vpiši prvo število",""));
var drugo=parseInt(prompt("Vpiši drugo število",""));
var vsota=prvo+ drugo;
var razlika=prvo-drugo;
var zmnozek=prvo*drugo;


if(prvo>0)
{
stil1="pozitivno";
}
else
{
stil1="negativno";
}
if(drugo>0)
{
stil2="pozitivno";
}
else
{
stil2="negativno";
}

if(prvo>drugo)
{
razmerje="večje ";
clen=" od";
}
else
{
if(prvo<drugo)
{
razmerje="manjše ";
clen=" od";
}
else
{
razmerje="enak ";
clen="";
}
}
a=false;
if (false==isNaN(prvo) && false==isNaN(drugo))
{
document.write("Prvo število je: <span class="+stil1+">"+prvo+"</span>");
document.write("<br/> Drugo število je: <span class="+stil2+">"+drugo+"</span>");
document.write("<br/> Vsota je "+vsota);
document.write("<br/> Razlika je "+razlika);
document.write("<br/> Zmnožek je "+zmnozek);
if (drugo!=0)
{
var kolicnik=prvo/drugo;
var ostanek=prvo%drugo;
document.write("<br/> Kolicnik je "+kolicnik);
document.write("<br/> Ostanek je "+ostanek);
}
else
{
			
document.write('<br><span class="negativno">Deljenje ni izvedljivo!</span><br />');	
document.write('<span class="negativno">Ostanek ni izvedljiv!</span>');		
}
document.write("<br/> A je <b>" + razmerje + "</b> "+clen+" B.");
}
else
	{
		if (true==isNaN(prvo))
			document.writeln('<span class="negativno">Vpisana vrednost za število A ni število!</span><br />');	
		if (isNaN(drugo)==true)
			document.writeln('<span class="negativno">Vpisana vrednost za število B ni število!</span><br />');	
	}
</script>
</head>
<body>

</body>
</html>

Ales47 ::

Hvala :)

Ales47 ::

problem je, da nebom znal to tocno zagovorit:) kratek opis programa, kjer kaj dela, maybe? da nebom ju3 zablodil :D

amacar ::

Maš kaki msn, skype, gmail talk, piši mi na amadej.pevec@gmail.com pa se zmeniva.

LP


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Javascript pomoč

Oddelek: Izdelava spletišč
201976 (1532) Lion29
»

Javascript problemcek

Oddelek: Programiranje
141508 (1301) tempoFlow
»

[JavaScript] Splošna shema programa

Oddelek: Programiranje
51093 (970) CWIZO
»

JavaScript naloge za faks (ustni izpit)

Oddelek: Šola
182252 (1792) Lumix
»

if flash else html....

Oddelek: Izdelava spletišč
101212 (1034) alesrosina

Več podobnih tem