DEV Community

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

Posted on

4

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay