๐Ÿ“Œ 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๋˜๋Š” ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ž‘์„ฑํ–ˆ๋‹ค. ๋‹ค์Œ์—๋Š” ์‚ผํ•ญ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•ด ๋ณผ ๊ฒƒ..!