skip to Main Content

by using

function convert(minutes) {
     
}

How can i change minutes to second

i was trying

function convert(minutes) {
    let sec
    sec = number(sec)
    let minutes = 60 * sec
    return()
}

and it doesnt work
so if any one of you know please help me

2

Answers


  1. Your function does not make any sense, as sec is not defined and the function itself returns undefined.

    Try it this way:

    const convert = (minutes) => 60 * Number(minutes);
    console.log(convert(3));
    Login or Signup to reply.
  2. There are a few problems with your code:

    function convert(minutes) {
    
        // you've initialised the variable, but it's not yet
        // defined:
        let sec
    
        // here you're converting undefined into a number, but
        // because JavaScript is case-sensitive, you're calling
        // a function - number() - that doesn't exist; I assume
        // you meant to use Number(); and then attempting to
        // assign the result - which never comes, because of the
        // "Uncaught ReferenceError: number is not defined"
        // - to the sec variable (your code has already broken by this
        // point though), also Number(sec) returns NaN (Not a Number),
        // any subsequent calculation will also fail, or error:
        sec = number(sec)
    
        // I have to assume you're converting minutes to seconds,
        // so the calculation is correct, but you're trying to
        // redeclare an existing variable, which is itself an error:
        // "Uncaught SyntaxError: redeclaration of formal parameter minutes"
        let minutes = 60 * sec
    
        // return isn't a function, and if it was you pass no arguments,
        // so here you're returning nothing; at best, this leads to
        // the calling context receiving 'undefined', but more likely
        // an error is generated: Uncaught SyntaxError: expected expression, got ')':
        return()
    }
    

    To fix that on the assumption – please clarify this sort of thing in the question – that you want to convert a number of minutes (say, 2) into seconds (for example 120):

    // here we're using an Arrow function (as the function doesn't need to use 'this'
    // anywhere; we're declaring the function as a const, taking one argument 'minutes',
    // using parseFloat (because I'm allowing for floats, such as 1.25, or 1.6 minutes
    // to be entered) and returning that number multiplied by 60 (the number of seconds
    // per minute):
    const convert = (minutes) => parseFloat(minutes) * 60;
    
    // finding the <form> element using document.querySelector(), and using
    // EventTarget.addEventListener() to bind the anonymous function as the
    // event-handler for the 'submit' event:
    document.querySelector('form').addEventListener('submit', (evt)=>{
      // this function automatically receives a reference to the Event
      // Object to which it's bound, and we use that reference to
      // prevent the browser's default response to a <form> submission:
        evt.preventDefault();
      
      // here we initialise the seconds variable, using let, and assign
      // the returned value of the convert function as it's value:
      let seconds = convert(
        // here we use document.querySelector() to retrieve the <input>
        // via its id ('m'), and submit its value (a string) to the
        // convert() function:
        document.querySelector('#m').value
      );
      
      // here we retrieve the <output> element, via its id, and set
      // its value to the vale of the variable 'seconds':
      document.querySelector('#s').value = seconds;
    });
    <form action="#" method="post">
      <fieldset>
        <legend>Convert</legend>
        <label>
          <span class="labelText">Minutes</span>
          <input type="number" min="0" max="120" value="1" step="0.25" id="m">
          </label>
        <button>Convert</button>
        <output id="s"></output>
      </fieldset>
    </form>

    JS Fiddle demo.

    As an aside, though, I’d strongly suggest you look up some JavaScript tutorials to learn the basics; the errors you present look like you’ve maybe seen, and probably read, some JavaScript but haven’t learned, or retained, enough information about, or understanding of, the language and its uses. This isn’t intended to be offensive, but it’s hard to progress without learning the basics (even if you’re learning as you go).

    References:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search