/    Sign up×
Community /Pin to ProfileBookmark

Help with some school

I can’t get this to work can someone please help.
I am completely lost with this one.?

  • 1.

    Have the user type in the first box a number to start counting from.


  • 2.

    Have the user type in the second box a number where to stop counting to.


  • 3.

    Have the user type in a number to step up/down by when counting.

  • *

    When the user presses the button this page will clear and the list of numbers will be displayed.


  • *

    If the user uses the same starting and ending number alert them that is not valid.


  • *

    The user can enter a starting number bigger than the ending number.


  • *

    Take into account that a user might enter a positive or negative step value

  • <html>
    <head>

    <script type=”text/javascript”>

    function add
    r=document.form2.text3.value
    j=document.form2.text4.value

    }
    for(I=r;I<=j;I=I+1){

    if(x==y){
    alert(“The numbers are the same”)
    }
    <script>
    </HEAD>
    <BODY>

    Statring Number <input type=”text” id=”start”>
    Ending Number <input type=”text” id=”end”>
    Step by <input type=”text” id=”step”>

    <input type=”button” onClick=”add”>

    </body>
    </html>

    to post a comment
    JavaScript

    13 Comments(s)

    Copy linkTweet thisAlerts:
    @skywalker2208Feb 23.2009 — I am not trying to be mean, but I think you need to go back to looking at your textbook. Your function declaration is wrong, you have two script opening tags and no closing script tags and a closing brace that doesn't have a starting opening brace, and a opening brace with no closing brace. You are trying to use the grab the form elements, but you don't have a form tag with the attribute name and your input fields don't have attribute names. They have id's where you could target them with the document.getElementById() method. This is why I say you should go back to your textbook and check out some examples.

    I don't mind helping students because I was in your position before, but I am not going to do your homework for you. I would suggest start small like getting the onclick event to work by having it present an alert message to show you it is working. Then work on getting the input fields information by alerting those in the onclick event.
    Copy linkTweet thisAlerts:
    @MitchmanauthorFeb 23.2009 — Thanks I will take a closer at it thanks
    Copy linkTweet thisAlerts:
    @MitchmanauthorFeb 24.2009 — Hi Skywalker

    Am I at least getting closer


    <HTML>

    <HEAD>

    <TITLE></TITLE>

    <script type="text/javascript">

    function ButtonPress(){

    x=document.getElementById("tb1")

    y=document.getElementById("tb2")

    if(x===y){

    alert("The numbers are the same")

    }

    else

    {

    for(I=x,I<=y,I=I+1)

    }

    document.write(I)

    document.write("<br/>")

    }

    </script>

    </HEAD>

    <BODY>

    <form name="form1">

    Starting Number <input type="text" id="tb1">

    Ending Number <input type="text" id="tb2">

    <input type="button" onClick="ButtonPress()">

    </form>


    </BODY>

    </HTML>
    Copy linkTweet thisAlerts:
    @ZeroKilledFeb 24.2009 — first you have error syntax, like not properly closing each structure (function body, loop body). second, [b]getElementById[/b] return an object element. in others words, [b]x[/b] and [b]y[/b] doesn't directly hold the value typed by the user, they are just a reference to that element. to read the content you have to read the [b]value[/b] property of each element: [b]x.value and y.value[/b]. and third, be aware that any value read from the document is considered as a string. that is, the [b]value[/b] property of [b]x[/b] and [b]y[/b] is of type string. you need to convert them to integer type so your loop can work correctly.
    Copy linkTweet thisAlerts:
    @JMRKERFeb 24.2009 — Close, but still no cigar ...

    Hints:

    1. x & y retrieved is the object pointer of the element, not the value

    2. x & y, when correctly retrieved, will be strings, not numbers

    3. Still have no checks for when y > x

    4. Proper check for equality of x and y is if (x == y) {

    5. No checks for if user enters "one" instead of '1' or "two" instead of '2'.

    Try fixing above before I post the answer.
    Copy linkTweet thisAlerts:
    @snowiekenFeb 24.2009 — Big tip when you are first starting with code like this: use indents. This way it becomes clear which closing braces you have forgotten, which lines of code are within which braces, etc.

    Also, what skywalker said, begin small and work your way up. Add an onclick event that shows a simple alert message and see if that works. If it does, change the alert message in a form value and see how that turns out. Then start to add if statements and loops. Don't try to write a whole bunch of code if you don't know what you are doing yet, take small steps at a time so you know exactly what to debug if it doesn't work.

    And yes, pay attention to your textbook because you have a lot of silly syntax errors (like triple = symbols for comparison, which should be double)
    Copy linkTweet thisAlerts:
    @skywalker2208Feb 24.2009 — Also try installing firebug for firefox because it is great tool to help you find syntax errors.
    Copy linkTweet thisAlerts:
    @MitchmanauthorFeb 25.2009 — Hi Guys,

    I have been putting a lot more time on the and this is what I can up with.

    The alert works fine. I get NaN when I put two different number is the textboxes.

    <HEAD>

    <TITLE></TITLE>

    <script type="text/javascript">

    function ButtonPress()

    {

    var x,y,z;

    x=document.getElementById("tb1").value;

    y=document.getElementById("tb2").value;

    z=document.getElementById("tb3").value;

    if(x==y) {

    alert("Your start and end numbers are the same")

    }

    else {

    document.write(parseInt(I=x,I<=y,I=I+1)+ "<br />");

    }

    }

    </script>

    </HEAD>

    <BODY>

    <form name="form1">

    Statring Number <input type="text" id="tb1">

    Ending Number <input type="text" id="tb2">

    Step By <input type="text" id="tb3">

    <input type="button" value="display numbers" onClick="ButtonPress()">

    </form>

    </BODY>

    </HTML>
    Copy linkTweet thisAlerts:
    @ZeroKilledFeb 25.2009 — the script is totally wrong. you receive [b]NaN[/b] because [b]parseInt[/b] allow only two arguments: the number string type and the base. you are passing as many arguments to intend converting them but that isn't how the function work. also, you are using document.write which will clear any content (including the script) of your document.
    Copy linkTweet thisAlerts:
    @MitchmanauthorFeb 25.2009 — I did not know ParseInt only allows two arguments. And I need you use document.write that's what my teacher want's me to use.
    Copy linkTweet thisAlerts:
    @ZeroKilledFeb 25.2009 — you have to rethink the logic of your code, specially the statement in [b]else[/b] body. some hint:

    * before comparing x and y, both variable has to be integer type

    *
    need a loop statement to repeat the writing in the document
    Copy linkTweet thisAlerts:
    @MitchmanauthorFeb 26.2009 — Hi ZeroKilled,

    Could you show me what you mean I just can't get this.

    I have been busting my buy for a couple day on this now.
    Copy linkTweet thisAlerts:
    @JMRKERFeb 26.2009 — Study on this;
    <i>
    </i>&lt;HTML&gt;
    &lt;HEAD&gt;
    &lt;TITLE&gt;Loop Test&lt;/TITLE&gt;

    &lt;script type="text/javascript"&gt;
    function ButtonPress() {
    x=Number(document.getElementById("tb1").value);
    if (isNaN(x)) { alert('Invalid input: '+x); return; }
    y=Number(document.getElementById("tb2").value);
    if (isNaN(y)) { alert('Invalid input: '+y); return; }

    if(x==y) {
    alert("The numbers are the same");
    } else {
    if (y &gt; x) {
    var str = '';
    for(I=x; I&lt;=y; I=I+1) { str += I+'n'; }
    alert(str);
    } else { alert('Start is bigger than ending number'); }
    }
    }
    &lt;/script&gt;

    &lt;/HEAD&gt;
    &lt;BODY&gt;

    &lt;form name="form1"&gt;
    Starting Number &lt;input type="text" id="tb1"&gt;
    Ending Number &lt;input type="text" id="tb2"&gt;

    &lt;input type="button" onClick="ButtonPress()" value="Loop Test"&gt;
    &lt;/form&gt;

    &lt;/BODY&gt;
    &lt;/HTML&gt;
    ×

    Success!

    Help @Mitchman spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.15,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...