Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:17:20+00:00 2026-06-15T23:17:20+00:00

My first day of Javascript and I am incredibly confused. I am passing numbers

  • 0

My first day of Javascript and I am incredibly confused. I am passing numbers returned from a form to a function, but the result is not consistent with what it should be. My testing leaves a lot to be desired, but hopefully the following makes sense.

The function g calculates the sum of the sequence.

 <form name="gaussform">
    <input name="min" 
           type="number"
           min="1"
           value="1">
    <input name="max" 
           type="number"
           min="2"
           value="10">
    <input name="step" 
           type="number"
           min="1"
           value="1">
    <input onclick="alert_g()"
           type="submit"
           value="calculate">
  </form>

  <script type="text/javascript">

    function g(min,max,step) {
    var actualmax = max - ((max - min) % step)
    return (min + actualmax) * ((1 + ((actualmax - min) / step)) / 2)
    }

    function alert_g() {
    var frm = document.forms["gaussform"]
    var min = frm["min"].value
    var max = frm["max"].value
    var step = frm["step"].value
    if (min == 1) {
    alert("min is 1")}
    if (max == 10) {
    alert("max is 10")}
    if (step == 1) {
    alert("step is 1")}
    alert(g(min,max,step))
    // below returns the desired result
    alert(g(1,10,1))}
  </script>

the if statements are only so I can understand what is going on!

So if the user enter 1,10,1 (the default values) the result should be 55.

alert(g(1,10,1)) -> 55

alert(g(min,max,step) -> 550

alert(g(1,100,2)) -> 2500

alert(g(min,max,step) -> 4975 (obviously min,max,step == 1,100,2)

the function g is correct, but I do not understand what is happening to the values that are being passed to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:17:21+00:00Added an answer on June 15, 2026 at 11:17 pm

    This expression…

    min + actualmax
    

    …is the problem.

    You probably want to make that…

    +min + +actualmax
    

    …or use parseFloat(), parseInt() or whatever suits your requirements to turn those strings into an actual Number.

    jsFiddle.

    JavaScript’s + operator is overloaded for arithmetic addition and string concatenation. Because user input is always a string, you’re doing string concatenation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript array of dates, in the form {year:'2010',month:'6',day:'23'} I need to
I'm facing at first sight a fairly trivial task but during a day I
It's my first day working with JavaScript, and I need help on a simple
I am trying to learn JavaScript. First day! I want to concatenate the First
Good day, first I would like to tell that I am not proud to
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Possible Duplicate: Get first and last day of the week in JavaScript Can any
Today is my first day in JavaScript. The book (JavaScript Definitive Guide) has an
First day at programming in Node today. I've been getting along nicely all day
First day and first attempt at using Scala - so go easy on me!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.