๐ 1. ๋ฌธ์
[Codewars, 7kyu] Square Every Digit
Welcome. In this kata, you are asked to square every digit of a number and concatenate them.
For example, if we run 9119 through the function, 811181 will come out, because 92 is 81 and 12 is 1.
Note: The function accepts an integer and returns an integer
Test.assertEquals(squareDigits(9119), 811181);
โ ํ์ด
function squareDigits(num) {
let result = [];
let array = num.toString().split("");
array.forEach((item) => {
result.push(item * item);
});
return parseInt(result.join(""));
}
- ์ซ์๋ฅผ ๋ฌธ์๋ก ๋ฐ๊ฟ์ ํ๋์ ๋ฐฐ์ด์ด ๋๋ ๋ณ์๋ฅผ ๋ง๋ค๊ณ ,
forEach
๋ก ๋ฐฐ์ด์ ๋๋ ค ๋น ๋ฐฐ์ด ๋ณ์์ ๊ณ์ฐ๋ ์์๊ฐpush
๋๋ ์ฝ๋๋ฅผ ์์ฑํ๋ค. ์ถ์ฒํ์ด์์๋map
์ ์ฌ์ฉํ์ฌ ๋น ๋ฐฐ์ด์ ๋ง๋ค์ง ์๊ณarrow function
์ผ๋ก ๋ฐ๋ก ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด๋๋ค. ๋งค๋ฒ ๊ฐ์ ๋ฉ์๋๋ง ์จ์ ๋ฌธ์ ํธ๋ ๋๋โฆ. ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทผํ์โฆ
๐ 2. ๋ฌธ์
[Codewars, 6kyu] Stop gninnipS My sdroW!
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Examples: spinWords( โHey fellow warriorsโ ) => returns โHey wollef sroirrawโ spinWords( โThis is a testโ) => returns โThis is a testโ spinWords( โThis is another testโ )=> returns โThis is rehtona testโ
Test.assertEquals(spinWords("Welcome"), "emocleW");
Test.assertEquals(spinWords("Hey fellow warriors"), "Hey wollef sroirraw");
Test.assertEquals(spinWords("This is a test"), "This is a test");
Test.assertEquals(spinWords("This is another test"), "This is rehtona test");
โ ํ์ด
function spinWords(item) {
let arr = [];
let b = item.split(" ");
b.forEach((a) => {
if (a.length >= 5) {
arr.push(a.split("").reverse().join(""));
} else {
arr.push(a);
}
});
return arr.join(" ");
}
- ๋ฌธ์์ ๊ธธ์ด๊ฐ 4๋ณด๋ค ํฌ๋ฉด ๋ค์ง์ด์
push
, ์์ผ๋ฉด ๊ทธ๋๋กpush
๋๋ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์์ฑํ๋ค. ๋ค์์๋ ์ผํญ์ฐ์ฐ์ ์ฌ์ฉํด ๋ณผ ๊ฒ..!