在 JavaScript 中根据出生日期查找生命路径编号
人生轨迹号
一个人的人生道路编号是通过将该人的出生日期中的每个数字相加,直到它减少到一位数来计算的。
问题
我们需要编写一个JavaScript函数,该函数接受“yyyy-mm-dd”格式的日期并返回该出生日期的生命轨迹编号。
例如,
如果日期是: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
示例
以下是代码-
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));输出结果以下是控制台输出-
8