Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding life path number based on a date of birth in JavaScript
Life Path Number
A person's Life Path Number is calculated by adding each individual number in that person's date of birth, until it is reduced to a single digit number.
Problem
We are required to write a JavaScript function that takes in a date in “yyyy-mm-dd” format and returns the life path number for that date of birth.
For instance,
if the date is: 1999-06-10
year : 1 + 9 + 9 + 9 = 28 → 2 + 8 = 10 → 1 + 0 = 1 month : 0 + 6 = 6 day : 1 + 0 = 1 result: 1 + 6 + 1 = 8
Example
Following is the code −
const date = '1999-06-10';
const findLifePath = (date = '') => {
const sum = (arr = []) => {
if(arr.length === 1){
return +arr[0]
};
let total = arr.reduce((acc, val) => acc + val);
if (total < 10){
return total
};
return sum(String(total).split("").map(Number));
};
let [year, month, day] = date.split("-")
year = sum(String(year).split("").map(Number));
month = sum(String(month).split("").map(Number));
day = sum(String(day).split("").map(Number));
return sum([year,month,day]);
};
console.log(findLifePath(date));
Output
Following is the console output −
8
Advertisements