JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2012, 05:08 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Other - New/learning javascript, hoping for clarification for this short piece of code please

Hello all,

Im new to Javascript so i'm trying to learn from the beginning. i wrote this, as a first attempt, but no joy. can anyone help point me in the right direction/ guide me on what im doing wrong please.

Code:
<head>
    <title>Untitled Page</title>
    <script type="text/javascript">

        function myfunction() {
            var d = new date();
            var day = d.getDate();
            if (day == 2) {
                alert("Tuesday")
            }
            else { 
                alert("some other day")
            }
        }

        
    </script>
</head>
<body>

<button type="button" onclick="myfunction()"> click me</button>

</body>
</html>


Your help as always is greatly appreciated.

Kind regards
MG

Reply With Quote
  #2  
Old October 2nd, 2012, 05:24 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,872 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 16 h 37 m 29 sec
Reputation Power: 2569
"getDate" returns the day of the month, i.e. 1 to 31. "getDay" will return 0 to 6 for the day of the week.
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
  #3  
Old October 2nd, 2012, 05:56 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hi, thanks for the reply.

I changed it to 'getDay', but that doesnt work either

Code:

        function myfunction() {
            var d = new date();
            var day = d.getDay();
            if (day == 2) 
            {
                alert("Tuesday")
            }
            else 
            { 
                alert("some other day")
            }
        }


regards
MG

Reply With Quote
  #4  
Old October 2nd, 2012, 06:18 AM
jwer jwer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 jwer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 36 sec
Reputation Power: 0
What if you wrote
var d = new Date();
with capital D?

Reply With Quote
  #5  
Old October 2nd, 2012, 06:23 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hi, thanks for the post.

So i changed it slightly and now its kind of working. the new code looks like this:

Code:
           var d = new date();
            var day = d.getDay();
        function myfunction() {
            
            if (day == 2) 
            {
                alert("Tuesday");
            }
            else 
            {
                alert("some other day");
            }
        }



As you can see the variables are now outside the function, but i thought that shouldnt matter? you can put variable in functions right?

but now it displays "some other day" ? isnt tuesday the number 2 from 0-6 using the getDay() method??

Kind regards
MG

Last edited by mind_grapes : October 2nd, 2012 at 06:26 AM.

Reply With Quote
  #6  
Old October 2nd, 2012, 06:39 AM
jwer jwer is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 jwer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 36 sec
Reputation Power: 0
Yes, you can, and, maybe should, define variables inside functions!
I've tried your code in my browser, and replaced
new date();
with
new Date();

It complained "date is not defined"; changed it to Date - happy browser.

best
jwer

Reply With Quote
  #7  
Old October 2nd, 2012, 07:35 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hello, cheers for reply.

I changed it like yourself, and now it works. I think it may have been due to it not being a capital 'D'. Caps hey!? :-).

thank you all for your help. You've been a real help as always.

Regards
MG

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - New/learning javascript, hoping for clarification for this short piece of code please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap