王荣胜

我需要多久实现财富自由?

2020-07-06 · 8 min read
大学生

直接使用Chorme浏览器Console运行代码即可~

let cost = {

    cloth:-1,

    eat:-1,

    house:-1,

    traffic:-1

}

let monthCost = 0;

let freedom = 0;

let yearSave = 0;

let monthSave = 0;

let old = -1;

let already = 0;



let checkType = (money,type)=>{

    if(money<0){

        console.warn('开销不能为负数');

        return -1;

    }

    if(type==='月') return money;

    if(type === '日') return money*30;

    if(type === '年') return Math.floor(money/12);

}



let money = (money) => {

    already = money;

}



let checkInfo = (funName)=> {

    let can = true;

    if(cost.cloth<0){

        console.warn('你还需要调用cloth(),告诉程序你在穿着打扮上的开销');

        can = false;

    }

    if(cost.eat<0){

        console.warn('你还需要调用eat(),告诉程序你在吃上的开销');

        can = false;

    }

    if(cost.house<0){

        console.warn('你还需要调用house(),告诉程序你在住上的开销');

        can = false;

    }

    if(cost.traffic<0){

        console.warn('你还需要调用traffic(),告诉程序你在交通出行上的开销');

        can = false;

    }

    if(!can) console.warn(`否则无法运行${funName}()`);

    return can;

}



let eat = (money,type='月')=>{

    cost.eat = checkType(money,type);

    return `每个月在吃上花${cost.eat}元`

}



let cloth = (money,type='月') => {

    cost.cloth = checkType(money,type);

    return `每个月在穿上花${cost.cloth}元`

}



let house = (money,type='月') => {

    cost.house = checkType(money,type);

    return `每个月在住上花${cost.house}元`

}



let traffic = (money,type='月') => {

    cost.traffic = checkType(money,type);

    return `每个月在行上花${cost.traffic}元`

}



let age = (age)=>{

    old = age;

    return `今年${age}岁`

}



let getValue = ()=>{

    let {cloth,eat,house,traffic} = cost;

    monthCost = cloth+eat+house+traffic;

    yearCost = monthCost * 12;

    freedom = Math.floor(yearCost/0.04);

}



let costSum = () => {

    if(!checkInfo('costSum')) return;

    getValue();

    freedom = Math.floor(yearCost/0.04);

    console.log(`月开销:%c${monthCost}`,'color:yellow');

    console.log(`年开销:%c${yearCost}`,'color:yellow');

    console.log(`财务自由需要存款%c${freedom}`,'color:yellow')

}



let keep = (income) => {

    if(!checkInfo('keep')) return;

    getValue();

    if(income<monthCost){

        console.warn(`你的月生活开销是${monthCost},而月收入是${income}` );

        console.warn(`你的收入无法支撑你的生活开销,请降低你的生活开销或提高你的收入` )

        return '你可以通过调用eat(),house(),cloth(),traffic()降低生活开销';

    }

    yearIncom = income*12;

    canSave = yearIncom - yearCost;

    let save = already;

    let temp = 0;

    i = 1;

    console.log(`你的总资产是:${already},若保持月收入${income}`);

    if(already>0) console.log('一开始便将闲置资产投入有收益的理财产品(如基金)')

    console.warn('每年历程如下!')

    while(save<freedom){

        temp = save;

        if(save<0){

            save+=canSave;

            console.log(`第${i}年后,总债务${temp},多余收入需要还债务,清还${canSave},总资产${save}`);

            if(save>0) console.log('此时便将资产投入理财产品(如基金)')

        }else{

            save+=canSave + save*0.04;

            console.log(`第${i}年后,存款产生收益${Math.floor(temp*0.04)},未操作前总存款${Math.floor(temp)},存入${canSave},总存款${Math.floor(save)}`);

        }

        i++;

    };

    console.log(`%c恭喜你,在第${i}年进入财务自由,总存款${Math.floor(save)},存款产生收益${Math.floor(save*0.04)}`,'color:red');

    console.log(`如果保持当前月收入%c${10000}%c,你需要%c${i}年%c的时间达到财务自由`,'color:lightgreen','','color:yellow','')

}



let aim = (aimOld)=>{

    if(!checkInfo('aim')) return;

    getValue();

    if(!aimOld){

        return '请通过运行调用本函数时,在aim()的括号中填入你希望多少岁达到财务自由,否则无法计算'

    }

    if(aimOld<old){

        return `时光无法倒流,请正确填入数字`

    }

    if(old<0){

        console.warn('由于您还为告诉系统您的正确年龄,所以无法计算');

        return '请通过运行 age(),在括号中填入年龄,来告诉程序您当前年龄'

    }

    if(already<0){

        return '目前暂不支持负资产调用本计算,因为我数学不太行- -'

    }

    howManyYear = aimOld - old

    m = 0;

    for(let i = 1;i<howManyYear;i++){

        m = m*0.04+m+1

    }

    yearSave = Math.floor((freedom-already)/m);

    monthSave = yearSave/12;

    console.log(`你的总资产是${already},今年${old}岁,想要在${aimOld}时财务自由`);

    console.log(`你需要在每年都存下${yearSave}`);

    console.log(`然而你每年的开销是${yearCost}`);

    console.log(`所以你的年薪至少需要是${Math.floor(yearCost+yearSave)}`);

    console.log(`所以需要每个月都存下${Math.floor(monthSave)}`);

    console.log(`然而你每个月的开销是${monthCost}`);

    console.log(`所以你每个月至少要赚${Math.floor(monthCost+monthSave)}`)

    let saving = already;

    console.warn('每年历程如下!')

    for(let i = 0;i<howManyYear;i++){

        let temp = saving;

        saving+=yearSave+(temp*0.04)

        if(i===howManyYear-1){

            console.log(`恭喜你,在第${i+1}年进入财务自由,总存款${Math.floor(temp)},存款产生收益${Math.floor(temp*0.04)}`);

            console.log(`如果想要在${24+howManyYear}时达到财务自由,你的月收入需要达到每个月${Math.floor(monthCost+monthSave)}`)

        }else{

            console.log(`第${i+1}年,存款产生收益${Math.floor(temp*0.04)},未操作前总存款${Math.floor(temp)},存入${yearSave},总存款${Math.floor(saving)}`);

        }

    }

}



let rule = ()=>{

    console.log(`%c欢迎使用财务自由小程序`,'color:yellow;font-size:20px');

    console.log(`%c-----------------第一步-----------------`,'font-size:15px');

    console.log(`首先使用cloth(),eat(),house(),traffic()四个方法告诉程序你的日常开销`);

    console.log(`四个函数分别对应着衣食住行`);

    console.log(`%ceat:%c告诉程序你花在吃上的开销`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%ceat(50,'日')`,'color:yellow');

    console.log(`在这个例子中,第一个参数50是指需要花50元,第二个参数'日'是指单位`);

    console.log(`%c也就是告诉程序,一天在吃上需要花费50元`,'color:yellow');

    console.log(`%ccloth:%c告诉程序你花在穿着打扮上的开销`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%ccloth(2000,'年')`,'color:yellow');

    console.log(`%chouse:%c告诉程序你花在住房上的开销`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%chouse(3000,'月')`,'color:yellow');

    console.log(`%ctraffic:%c告诉程序你花在吃上的开销`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%ctraffic(200,'月')`,'color:yellow');

    console.log(`%c具体方法请查看视频!`,'color:red');

    console.log(`%c------------------第二步-----------------`,'font-size:15px');

    console.log(`完成上述操作后,可使用costSum(),keep(),aim()函数来查看计算结果`);

    console.log(`%ccostSum:%c计算月开销和年开销以及财务自由所需的存款`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%ccostSum()`,'color:yellow');

    console.log(`%ckeep::%c计算保持现有每月收入的情况下,多久达到财务自由`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%ckeep(5000)`,'color:yellow');

    console.log(`%caim:%c计算如果想要在目标年龄实现财务自由,平均需要月入多少`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%caim(30)`,'color:yellow');

    console.log(`%cage:%c设置你的年龄`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%cage(18)`,'color:yellow');

    console.log(`%cmoney:%c设置你的存款`,'color:lightgreen;font-wight:bold','');

    console.log(`例:%cmoney(1000)`,'color:yellow');

    console.log(`%c具体方法请查看文档!`,'color:red');

}

rule();
小白
本文作者:王荣胜 |「邮箱 」| 「QQ」 | 「QQ群
文章出处:https://sqdxwz.top/
版权声明:本文章采用「知识共享署名-相同方式共享 4.0 国际许可协议」许可。
Powered by 王荣胜 | | 载入天数...载入时分秒...
Google     Server provider     jsDelivr    
正在加载今日诗词....