[04.16.22] Codewars

Create Phone Number

Description

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"

My answer

function createPhoneNumber(numbers){
  return `(${numbers[0]}${numbers[1]}${numbers[2]}) ${numbers[3]}${numbers[4]}${numbers[5]}-${numbers[6]}${numbers[7]}${numbers[8]}${numbers[9]}`
}

Other solutions

function createPhoneNumber(numbers){
  var format = "(xxx) xxx-xxxx";
  for(var i = 0; i < numbers.length; i++) {
    format = format.replace('x', numbers[i]);
  }
  return format;
}
function createPhoneNumber(numbers){
  numbers = numbers.join('');
  return '(' + numbers.substring(0, 3) + ') ' 
      + numbers.substring(3, 6) 
      + '-' 
      + numbers.substring(6);
}
function createPhoneNumber(numbers){
  return numbers.join('').replace(/(...)(...)(.*)/, '($1) $2-$3');
}

Playing with digits

Description

Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 1
695 --> 6² + 9³ + 5⁴= 1390 = 695
2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p.
we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k
n.

My answer

function digPow(n, p){
  let array = n.toString().split('').map(n => Number(n));
  let k = p;
  let calculatedArray = [];
  for (let i = 0; i < array.length; i++) {
    calculatedArray.push(array[i]**k);
    k++;
  }
  let result = calculatedArray.reduce((a,b) => a+b, 0) / n;
  return (result % 1 === 0) ? result : -1;
}

Other solutions

function digPow(n, p) {
  var x = String(n).split("").reduce((s, d, i) => s + Math.pow(d, p + i), 0)
  return x % n ? -1 : x / n
}
function digPow(n, p){
  var ans = (''+n).split('')
    .map(function(d,i){return Math.pow(+d,i+p) })
    .reduce(function(s,v){return s+v}) / n
  return ans%1 ? -1 : ans    
}

Wrap up

Math.pow

Math.pow(base, exponent)

Parameters

base

base number, 밑

exponent

exponent, 지수

Examples

// simple
Math.pow(7, 2);    // 49
Math.pow(7, 3);    // 343
Math.pow(2, 10);   // 1024
// fractional exponents
Math.pow(4, 0.5);  // 2 (square root of 4)
Math.pow(8, 1/3);  // 2 (cube root of 8)
Math.pow(2, 0.5);  // 1.4142135623730951 (square root of 2)
Math.pow(2, 1/3);  // 1.2599210498948732 (cube root of 2)
// signed exponents
Math.pow(7, -2);   // 0.02040816326530612 (1/49)
Math.pow(8, -1/3); // 0.5
// signed bases
Math.pow(-7, 2);   // 49 (squares are positive)
Math.pow(-7, 3);   // -343 (cubes can be negative)
Math.pow(-7, 0.5); // NaN (negative numbers don't have a real square root)
// due to "even" and "odd" roots laying close to each other,
// and limits in the floating number precision,
// negative bases with fractional exponents always return NaN
Math.pow(-7, 1/3); // NaN

source: mdn web docs

Persistent Bugger

Description

Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
For example (Input --> Output):
39 --> 3 (because 39 = 27, 27 = 14, 14 = 4 and 4 has only one digit)
999 --> 4 (because 9
99 = 729, 729 = 126, 126 = 12, and finally 12 = 2)
4 --> 0 (because 4 is already a one-digit number)

My answer

function persistence(num) {
  let result = 0;
  let test = num;
  
  while (String(test).split('').length > 1) {
    test = String(test).split('').map(a => Number(a)).reduce((a,b) => a*b, 1);
    result++;
  }

  return result;
}

Other solutions

const persistence = num => {
  return `${num}`.length > 1 
    ? 1 + persistence(`${num}`.split('').reduce((a, b) => a * +b)) 
    : 0;
}
function persistence(num) {
   for (var i = 0; num > 9; i++) {
     num = num.toString().split('').reduce((t, c) => c * t);
   }
   return i;
}

좋은 웹페이지 즐겨찾기