School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,578 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,225 people online right now. Registration is fast and FREE... Join Now!




javascript help

 

javascript help

Elbrus

18 Jun, 2009 - 02:15 PM
Post #1

New D.I.C Head
*

Joined: 22 Jul, 2008
Posts: 16

i just started learning javascript and i wanted to TRY to use what i learned to make a pointless program for testing what i know...
did not work so what i tried to do was test a switch statment and right out the result on the page onload here is the code, it probilly look's really bad to a pro but i just started so dont make fun of me to much wink2.gif

CODE

<html>
<head>
<script type="text/javascript">

function onLoad(){
var i;
var resault
switch(i=two;) {
case 'one';
case 'two';
    resault='OK';
    break;
case 'three';
    resault='OK2';
    break;
default;
    resault='NONE';
}
document.write(resault);
}


</script>
</head>
<body onload="onLoad">
</body>

</html>



User is offlineProfile CardPM
+Quote Post


SoLi

RE: Javascript Help

18 Jun, 2009 - 02:27 PM
Post #2

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
when binding the function onLoad() to the onload event of the body element, you need to use parenthesis so that the function instantiates:

CODE

<body onload="onLoad()">


the line declaring the variable resault needs to have a trailing semi-colon:

CODE

var resault;


when you are assigning the subject string to be checked in your case expression, you need to use single or double quotes, all strings must be contained in one of these.

CODE

switch(i="two") {


within a switch statement, each case needs to end in a colon. the same is true for the default case

e.g.
CODE

case "three":


Although this appears to be an exercise (so you have assigned referenced the subject string to the variable i within the call to switch() itself) your demonstration of switch's purpose might be clearer if you assign "two" to i before you get to switch():

CODE

function onLoad(){
  var i = "two";
  var resault
  switch(i) {
    case 'one':
    case 'two':
      resault='OK';
      break;
    case 'three':
      resault='OK2';
      break;
    default:
      resault='NONE';
  }
  document.write(resault);
}


let me know how it goes!

This post has been edited by SoLi: 18 Jun, 2009 - 02:36 PM
User is offlineProfile CardPM
+Quote Post

Elbrus

RE: Javascript Help

18 Jun, 2009 - 02:41 PM
Post #3

New D.I.C Head
*

Joined: 22 Jul, 2008
Posts: 16

i changed my code accordingly but still nothing... i thinking I may just not be getting a small thing.
here is my new code
EDIT: fixed the problime

i had a simple error in the body element i had to change a simple thing


CODE

<body onload="onLoad()">

simply just had to add the () i knew it was going to be something in the end very small biggrin.gif


CODE

<html>
<head>
<script type="text/javascript">

function onLoad(){
var i="two";
var result;
switch(i) {
case "one":
case "two":
    result="OK";
    break;
case "three":
    result="OK2";
    break;
default:
    result="NONE";
}
document.write(result);
}


</script>
</head>
<body onload="onLoad">
</body>

</html>


This post has been edited by Elbrus: 18 Jun, 2009 - 02:48 PM
User is offlineProfile CardPM
+Quote Post

SoLi

RE: Javascript Help

18 Jun, 2009 - 02:46 PM
Post #4

andydust.com
*****

Joined: 27 Jan, 2002
Posts: 1,428



Thanked: 35 times
My Contributions
Hi. Sorry I may have been editing my post whilst you checked it.

Here's the tip you're missing:

when binding the function onLoad() to the onload event of the body element, you need to use parenthesis so that the function instantiates:
CODE

<body onload="onLoad()">


User is offlineProfile CardPM
+Quote Post

Elbrus

RE: Javascript Help

18 Jun, 2009 - 02:50 PM
Post #5

New D.I.C Head
*

Joined: 22 Jul, 2008
Posts: 16

QUOTE(SoLi @ 18 Jun, 2009 - 02:46 PM) *

Hi. Sorry I may have been editing my post whilst you checked it.

Here's the tip you're missing:

when binding the function onLoad() to the onload event of the body element, you need to use parenthesis so that the function instantiates:
CODE

<body onload="onLoad()">




lol i just figured that out was editing post probably while you were posting yours thanks for the help mate! smile.gif

Great site this is great job dream.in.code devs biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 08:20AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month