[04.22.22] Coding test

Juyeon.it·2022년 4월 22일
0

Coding test

목록 보기
11/32

Human Readable Time

Description

Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)
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

function humanReadable (seconds) {
  let second = (seconds%60 < 10) ? `0${parseInt(seconds%60)}` : seconds%60;
  let minute = ((parseInt(seconds/60))%60 < 10) ? `0${parseInt(parseInt(seconds/60))%60}` : parseInt(seconds/60)%60;
  let hour = (parseInt(seconds/3600) < 10) ? `0${parseInt(seconds/3600)}` : parseInt(seconds/3600);
  
  return hour + ':' + minute + ':' + second;
}

Other solutions

function humanReadable(seconds) {
  var pad = function(x) { return (x < 10) ? "0"+x : x; }
  return pad(parseInt(seconds / (60*60))) + ":" +
         pad(parseInt(seconds / 60 % 60)) + ":" +
         pad(seconds % 60)
}
function humanReadable(seconds) {
  return [seconds / 3600, seconds % 3600 / 60, seconds % 60].map(function(v) {
    v = Math.floor(v).toString();
    return v.length == 1 ? '0' + v : v;
  }).join(':');
}
function humanReadable(seconds) {
  const oneMinute = 60
  const oneHour = oneMinute * 60
  const H = ('0' + Math.floor(seconds / oneHour)).slice(-2)
  const M = ('0' + Math.floor(seconds / oneMinute) % 60).slice(-2)
  const S = ('0' + seconds % 60).slice(-2)
  return `${H}:${M}:${S}`
}

Does my number look big in this?

Description

A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).
For example, take 153 (3 digits), which is narcisstic:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

and 1652 (4 digits), which isn't:

1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938

The Challenge:
Your code must return true or false (not 'true' and 'false') depending upon whether the given number is a Narcissistic number in base 10. This may be True and False in your language, e.g. PHP.
Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function.

My answer

function narcissistic(value) {
  let valueArray = `${value}`.split('').map(a => Number(a));
  let sum = valueArray.map(a => Math.pow(a, `${value}`.length)).reduce((a,b) => a+b, 0);
  return value === sum;
}

Other solutions

function narcissistic( value ) {
  return ('' + value).split('').reduce(function(p, c){
    return p + Math.pow(c, ('' + value).length)
    }, 0) == value;
}

Convert string to camel case

Description

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).
Examples
"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

My answer

function toCamelCase(str){
  let array = str.split(/[_,-]/);
  return array.map(function(a, ind) {
    return (ind === 0) ? a : a.charAt(0).toUpperCase() + a.slice(1);
  }).join('');
}

Other solutions

function toCamelCase(str){
      var regExp=/[-_]\w/ig;
      return str.replace(regExp,function(match){
            return match.charAt(1).toUpperCase();
       });
}

0개의 댓글

관련 채용 정보