νλ‘κ·Έλλ¨Έμ€ λ¬Έμ νμ΄ - μ½λΌμΈ μΆμΈ‘ / μμ°μ λ€μ§μ΄ λ°°μ΄λ‘ λ§λ€κΈ° / νλ ¬μ λ§μ / μ μ μ κ³±κ·Ό νλ³ / μ μΌ μμ μ μ κ±°νκΈ°
Math.sqrt() / Math.pow()
π λ΄κ° μμ±ν νμ΄
function solution(num) {
let result = num;
let count = 0;
if (result === 1) {
return 0;
}
while (result > 1 && count < 500) {
if (result % 2 === 0) {
result = result / 2;
count++;
} else if (result % 2 !== 0) {
result = result * 3 + 1;
count++;
}
}
if (count === 500) {
return -1;
}
return count;
}
π λ€λ₯Έ μ¬λμ νμ΄
λ΄ νμ΄μ λΉμ·νμ§λ§ ν¨μ¬ κΉλνλ€
(λ΄ νμ΄) κ΅³μ΄ μλ‘μ΄ result λ³μλ₯Ό λ§λ€μ΄μ numμ ν λΉν νμκ° μμλ€.
(λ΄ νμ΄) while λ¬Έκ³Ό return κ° λΆλΆμμ μ‘°κ±΄λΆ μΌν μ°μ°μλ‘ κ°κ²°νκ² νν κ°λ₯νλ€.
function solution(num) {
let count = 0;
while (num > 1 && count < 500) {
num%2 === 0 ? num = num/2 : num = num*3+1;
count++;
}
return num === 1 ? count : -1;
}
π λ΄κ° μμ±ν νμ΄
function solution(n) {
return (n + '').split('').map(s => Number(s)).reverse();
}
π λ€λ₯Έ μ¬λμ νμ΄
function solution(n) {
var arr = [];
do {
arr.push(n % 10);
n = Math.floor(n / 10);
} while (n > 0);
return arr;
}
π μ²μ μμ±ν νμ΄
μμ ν μ€νΈλ μ λΆ ν΅κ³Όνλλ° μ€μ μ±μ μμ 0μ λ§μλ€.
μ§μ§ μμ ν μ€νΈλ§ κ³ λ €ν λ΅μμ΄μλ€. arr1[0].lengthκ° 3 μ΄μμ΄λ©΄ 무쑰건 μ€λ΅μ΄ λμ¬ μλ°μ.
function solution(arr1, arr2) {
const arr = [];
arr1.forEach((item, index) => {
if (arr1[0].length === 1) {
const a = arr1[index][0] + arr2[index][0];
arr.push([a]);
} else {
const a = arr1[index][0] + arr2[index][0];
const b = arr1[index][1] + arr2[index][1];
arr.push([a, b]);
}
});
return arr;
}
π λ λ²μ§Έ μμ±ν νμ΄
for 루νλ‘ λ°©ν₯μ λ°κΏ¨λ€.
ν¨μ¨μ± ν μ€νΈκ° μμ΄μ ν΅κ³Όλ νμ§λ§, μκ°μ΄ λ§μ΄ κ±Έλ¦°λ€.
function solution(arr1, arr2) {
const answer = [];
for (let i = 0; i < arr1.length; i++) {
const arr = [];
for (let j = 0; j < arr1[0].length; j++) {
const elem = arr1[i][j] + arr2[i][j];
arr.push(elem);
}
answer.push(arr);
}
return answer;
}
π λ€λ₯Έ μ¬λμ νμ΄
function solution(arr1, arr2) {
return arr1.map((a, i) => a.map((b, j) => b + arr2[i][j]));
}
π λ΄κ° μμ±ν νμ΄
function solution(n) {
let x = 1;
while (x <= n) {
if (x ** 2 === n) {
return (x + 1) ** 2;
} else {
if (x ** 2 > n) {
return -1;
}
x++;
}
}
}
π λ€λ₯Έ μ¬λμ νμ΄
switch λ¬Έκ³Ό Math.sqrt(), Math.pow() νμ©
ν¨μ¨μ±μ΄ ν¨μ¬ μ’μ νμ΄λ€.
function solution(n) {
switch (n % Math.sqrt(n)) {
case 0:
return Math.pow(Math.sqrt(n) + 1, 2);
default:
return -1;
}
}
π‘ Math.sqrt()μ Math.pow()
// square root(μ κ³±κ·Ό) const sqrt = Math.sqrt(16); console.log(sqrt); // 4 // power(κ±°λ μ κ³±) const pow = Math.pow(3, 4); console.log(pow); // 81 (3β΄)
π λ΄κ° μμ±ν νμ΄ 1
function solution(arr) {
if (arr.length === 1) {
return [-1];
}
let minIndex = 0;
let nextIndex = minIndex + 1;
while (nextIndex < arr.length) {
if (arr[minIndex] > arr[nextIndex]) {
minIndex = nextIndex;
}
nextIndex++;
}
arr.splice(minIndex, 1);
return arr;
}
π λ΄κ° μμ±ν νμ΄ 2
function solution(arr) {
if (arr.length === 1) {
return [-1];
}
const cloneArr = [...arr];
cloneArr.sort((a, b) => a - b);
const minIndex = arr.indexOf(cloneArr[0]);
arr.splice(minIndex, 1);
return arr;
}
π λ€λ₯Έ μ¬λμ νμ΄
function solution(arr) {
if (arr.length === 1) {
return [-1];
}
arr.splice(arr.indexOf(Math.min(...arr)), 1);
return arr;
}
νλ‘κ·Έλλ¨Έμ€ λ¬Έμ νμ΄
μ± κ³μ μ½κΈ°