Js--codewars--human readable Time-math object, parseint ()

Source: Internet
Author: User
Tags square root

Problem Description:

Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format ( c0/>)

    • HH= hours, padded to 2 digits, range:00-99
    • MM= minutes, padded to 2 digits, range:00-59
    • SS= seconds, padded to 2 digits, range:00-59

The maximum time never exceeds 359999 ( 99:59:59 )

You can find some examples in the test fixtures.

My answer:

1 functionhumanreadable (seconds) {2   //TODO3   varhh=0; mm=0; Ss=0;4   if(seconds>359999) {return false;}5Hh=parseint (seconds/3600);6Mm=parseint ((seconds-hh*3600)/60);7Ss=parseint ((seconds-hh*3600)%60);8Hh= (hh<10)? " 0 "+hh:hh;9Mm= (mm<10)? " 0 "+mm:mm;TenSs= (ss<10)? " 0 "+Ss:ss; One     return(hh+ ":" +mm+ ":" +SS). toString ();  A}

Excellent answer:

1 function humanreadable (seconds) {2   var function return (x < 10)? "0" +x:x;} 3   return pad (parseint (seconds/(60*60)) + ":" +4          pad (parseint (SECONDS/60)) + ":" +5           pad (seconds%)6 }

JS take the whole to take the remainder

Http://www.jb51.net/article/50005.htm

Method Meaning Results
parseint (5/2); Discard the fractional part, preserving the whole number of parts 2
Math.ceil (5/2); Rounding up, with decimals on the entire number of parts +1 3
Math.Round (5/2); Rounded 3
Math.floor (5/2); Rounding down 2

1, conversion function (valid for string type, otherwise non)

parseint ()//Convert a string to an integer

parseint ("123blue"); Return 123

parseint ("22.5"); return 22

Parsefloat ()//Convert a string to a floating-point number

Parsefloat ("1234blue"); Return 1234.0

Parsefloat ("22.5"); return 22.5

2, forcing type conversions

Boolean (value)----Converts the given value to a Boolean type

The Boolean () function returns True when the value to be converted is a string of at least one character, a non-0 number, or an object (this is discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns FALSE.

Number (value)----Converts the given value to a digit (which can be an integer or a floating point)

string (value) ----to convert the given value to a string

Weak-type conversion of 3,JS (arithmetic operations only, string-to-number conversion is implemented)

var str= "012.345";

x=x*1; Return number 12.345

4. Methods for other commonly used math objects

Math.Abs (-1); 1

Math.log (1); Logarithm of base E, 0

Math.max (a); 3

Math.min (1,23); 1

Math.random (); Returns the number of random numbers between 0~1

Math.pow (2,3); 2 of the 3-time Square is 8.

MATH.SQRT (9,3); The square root of 8 is 2

Js--codewars--human readable Time-math object, parseint ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.