View Full Version : Where is the problem?I cant see it thats for shure.Please help me find it.
Jojosh
04-12-2004, 01:19 AM
Hey I know maybe it sound stupid but Im learning JS not for long.For learning I use book Sams teach yourself JavaScript in 24 h.I found it imposible ti complete it in 24 hours.It taked me 3 days to get to day 14 ;)
Now to the question.I tried to do this in any way I coul imagine but it just wont work.Why.What Im doing wrong.In my mind everything is ok here.But besouse it dosent work Im asking you politely to help me understand.Every thime I try to start this script IE gives me error(Object expected in line 27 char 1).What this mean.Is function wrong.Or something in HTML.I looked it over and over and I cant find error.Please help.
<html>
<head>
<title>Looping Arrays</title>
<script language="JavaScript1.5">
function listing(){
for(i=0;i<10;i++){
if(score[i]==5) document.write("Not passed");
document.write("Student number: " + i + "Scored: " + score[i] + "<br>");
}
}
score=new Array(10);
score[0]=6;
score[1]=5;
score[2]=7;
score[3]=8;
score[4]=7;
score[5]=10;
score[6]=5;
score[7]=6;
score[8]=9;
score[9]=8;
</script>
</head>
<body>
<h1>Rezultati<h1>
<form>
<input type="button" value="Pregled" onClick="listing()">
</form>
</body>
</html>
Idea is this.You click on button and you are shown student number and result.
shlagish
04-12-2004, 02:49 AM
When I simply change:
<script language="JavaScript1.5">
to
<script type="text/javascript">
it works..
My guess is that javascript 1.5 doesn't support arrays.
Jojosh
04-12-2004, 03:59 AM
Thank you wery much man.Im grateful for your help.But can you just explain me hoe is that JS 1.5 dont support arrays.Isnt 1.5 the best and newest.J.S
And what <text/javascript> do that <language=Javascript> do not?
shlagish
04-12-2004, 04:04 AM
Quite frankly, I don't know...
I know that I've been told to use type="text/javascript" instead of language="javascript1.5"
But I was only told it was "depricated".
That was a guess though, I don't know why it doesn't work...
shlagish
04-12-2004, 04:19 AM
hmm, I just noticed the script still doesn't work in mozilla..
Can't figure out why..
While I'm at it, there's a few things wrong with your script:
each time you create a variable, you should use var:
for(var i=0;i<10;i++){
...
var scores=new Array(10);
your if statement should have opening and closing curly braces:
if(score[i]==5){ document.write("Not passed"); }
Also, I'd try creating your array before creating the function that uses it...
And also, using indentation increases the code's visibility.
Also, when posting a code, feel encouraged to surround it with [ code] and [ /code] (without spaces).
Also, maybe everything should be written at once:
var content;
for(var i=0;i<10;i++){
content+="Student "+i+" got "+scores[i]+".";
if(scores[i]==5){
content+="He failed.";
}
}
document.write(content);
With all those modifications:
<html>
<head>
<title>Looping Arrays</title>
<script type="text/javascript">
function listing(){
var score=new Array(10);
score[0]=6;
score[1]=5;
score[2]=7;
score[3]=8;
score[4]=7;
score[5]=10;
score[6]=5;
score[7]=6;
score[8]=9;
score[9]=8;
var contents="";
for(var i=0;i<10;i++){
if(score[i]==5){
contents+="Failed: ";
}
contents+="Student number " + i + " scored " + score[i] + "<br>");
}
}
</script>
</head>
<body>
<h1>Rezultati<h1>
<form>
<input type="button" value="Pregled" onClick="listing()">
</form>
</body>
</html>
Jojosh
04-12-2004, 04:19 AM
Wow,you are fast.Please can you tell me how long are you learning JS.Is it very hard to master it withaout previous programming exp. How much time are nedded to achive a construcitve progress.(I mean how much time is necessary to say(I know it now).Is JS the hard,very hard, easy in your opinion.Sorry about pestering but if you answer Ill fell better:)Thanks
shlagish
04-12-2004, 04:23 AM
Still not working though, and I don't know why...
glenngv
04-12-2004, 04:33 AM
<html>
<head>
<title>Looping Arrays</title>
<script type="text/javascript">
function listing(){
var text = '';
for(var i=0;i<10;i++){
if(score[i]==5){
text+="Not passed<br />";
}
else {
text+="Student number: " + i + " Scored: " + score[i] + "<br />";
}
document.getElementById('result').innerHTML=text;
}
}
var score=new Array();
score[0]=6;
score[1]=5;
score[2]=7;
score[3]=8;
score[4]=7;
score[5]=10;
score[6]=5;
score[7]=6;
score[8]=9;
score[9]=8;
</script>
</head>
<body>
<h1>Rezultati<h1>
<form>
<input type="button" value="Pregled" onClick="listing()" />
<div id="result"></div>
</form>
</body>
</html>
shlagish
04-12-2004, 04:34 AM
Wow,you are fast.Please can you tell me how long are you learning JS.Is it very hard to master it withaout previous programming exp. How much time are nedded to achive a construcitve progress.(I mean how much time is necessary to say(I know it now).Is JS the hard,very hard, easy in your opinion.Sorry about pestering but if you answer Ill fell better :) Thanks
I've been learning javascript for I'm guessing about a year or two... I don't remember. But I was just learning a little here, a little there mostly on www.webmonkey.com, then at www.w3schools.com
There were long periods where I didn't do ANY javascript for a few months, so hard to tell how much time it took me.
This is my first programming language, and I find it very "logical" which I like:) It's fairly easy to learn because it follows logic.
I wouldn't say I master it, but I'm good enough ;)
If you wanna see someone who MASTERS js, go see glenngv ;) lol
So yeah, I'd say js is easy if you put enough time into it. But it can also be quite frustrating because sometimes, if you forget a comma, NOTHING will work.
If you're trying to learn js, I would suggest www.w3schools.com, combined with frequent trips to www.codingforums.com :)
What I like to do is put myself to the test.
For example, I said to myself:
Are you able to redirect the user to a different page depending on what he enters in a prompt you give him?
I tryied doing it, learned a whole bunch of stuff in the process, and now I can say, "yes, I'm able".
That was at the beggining, now it's more like:
"are you able to make a javascript version of excel?"
I just finished that, lol, very hard, anyways.
Test yourself and go beyond your limits, this forum will become your best friend, use the search function, use google..
Those are my suggestions, lol, enough typing now :)
Jojosh
04-12-2004, 05:02 AM
:confused: Hey does anybody out there knows is the "sams teach yourself Javascript in 24 h" book any good.Doesent look bad to me.Bur what you more(i should say very,very more )experienced people think.Is this book godo learning start or isnt.?What is the best bokk for a newbie to learn JS.Anyone?
shlagish
04-12-2004, 05:05 AM
I don't know that book, I havn't had any js book...
Jojosh
04-12-2004, 05:18 AM
Hey,I know is ridiculous question coming from me,but I recently stumbled on some JS games,and notices that they are rather simple(not in meaning simple to make them but to play them),I myself dont dream dreams of making some in near future but I cant help myself wondering.Is it possible to make game in Js like Pirates! or Elite,why just pacmen or tetris.In my humble opinion and knowlege of JS ,I think that someone that knows JS should be able to do this.Is JS powerful enough to lets say someone with exelent exp. in Js programming could make Elite in JS or Pirates!
shlagish
04-12-2004, 05:25 AM
I've seen mario bros done in js, I don't know what the limit is though...
But for complexe games, js probably isn't the best language to use..
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.