DEV Community

Cover image for Day 01: 30 Days of Codewars.js
Youssef Rabei
Youssef Rabei

Posted on

Day 01: 30 Days of Codewars.js

Table Of Contents


String ends with? : ✍ by jhoffner

📃 Description

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

🤔 Thinking

I wasn't thinking right at first.

I wanted to make an array with the end of the given string and then compare it to the given ending by pushing end length times

👨‍💻 Code

const solution = (str, ending) => {
  const arr = str.split("");
  let endingL = 0;
  let endingArr = [];

  while(endingL < ending.length) {
    endingArr.push(arr.pop());
    endingL += 1;
  }

  let endingStr = endingArr.reverse().join("");

  return ending === endingStr ? true : false;
}
Enter fullscreen mode Exit fullscreen mode

🐞 Bugs

  • I think it's the time complexity
  • Too much code for a simple task

🏁 Finally

Right After submitting my answer, my internet connection lost and I didn't even have the chance to read other people solutions, So I had the time to laugh on myself and see how stupid I was 🤣, And I remembered the substr method

So after the internet is back I submited this code

const solution = (str, end) => str.substr(str.length-end.length) === end;
Enter fullscreen mode Exit fullscreen mode

Does my number look big in this? : ✍ by JulianNicholls

📃 Description

A Narcissistic Number is a 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).

🤔 Thinking

I have to turn the number into an array so I can map over it and power every digit with number length and then add it with the reduce method and then check if it's equal to the original number

I removed the map and made it all with the reduce

👨‍💻 Code

const narcissistic = num => {
  const arrOfDigits = Array.from(String(num), Number);
  const pow = arrOfDigits.length;

  const sum = arrOfDigits.reduce((acc, val) => val ** pow + acc, 0 )

  return sum === num;
}
Enter fullscreen mode Exit fullscreen mode

Now I ranked up to 7kyu So I have the ability to mark the comments as having spoiler code

Top comments (0)