CodeSignal挑战:电话

时间:2018-09-11 15:47:32

标签: javascript ecmascript-6

challenge是:

某些电话的使用率可以描述如下:

  • 通话的第一分钟收费1美分
  • 从2号到10号(包括10分钟)的每分钟花费min2_10美分
  • 第十分钟后的每一分钟收费11美分。

在通话之前,您的帐户中有美分。您可以拥有的最长通话时间(以分钟为单位,四舍五入为最接近的整数)是什么?

我的代码:(我的代码未通过3个隐藏测试)

function phoneCall(min1, min2_10, min11, s) {
    
    //Declaring Variables
    let minutes = 0;
    let taken = 0;
    
    //Checks if there is atleast one minute to charge for
    if (s >= min1) {
        minutes = minutes + 1;
        s = s - min1;
        //Console Update
        console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
    }
    
    //Checks how many minutes it can call for in between 2 and 10
    for (let i = 0; i < 9; i++) {
        if (s > 1) {
            minutes = minutes + 1;
            s = s - min2_10;
            //Console Update
            console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
        } 
        
        
        //Checks if minutes equals 10 and then checks for how many minutes it can afford
        if (minutes === 10) {
            if (s >= 1) {
                minutes = minutes + Math.floor(s / min11);
                taken = taken + Math.floor(s / min11); 
                s = s - (taken * min11);
                //Console Update
                console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
            }
        }
    }
    
    //Returns the minutes
    return minutes;
}

1 个答案:

答案 0 :(得分:0)

刚刚发现什么不起作用!

if语句被写为“ if(s> 1){}”,但由于无法在所有类型的输入中使用而不能正确编写,因此我将其更改为“ if(s> = min2_10){ }”。

这是代码:

function phoneCall(min1, min2_10, min11, s) {
    
    //Declaring Variables
    let minutes = 0;
    let taken = 0;
    
    //Checks if there is atleast one minute to charge for
    if (s >= min1) {
        minutes = minutes + 1;
        s = s - min1;
        //Console Update
        console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
    }
    
    //Checks how many minutes it can call for in between 2 and 10
    for (let i = 1; i < 10; i++) {
        if (s >= min2_10) {
            minutes = minutes + 1;
            s = s - min2_10;
            //Console Update
            console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
        } 
        
        
        //Checks if minutes equals 10 and then checks for how many minutes it can afford
        if (minutes === 10) {
            if (s >= 1) {
                minutes = minutes + Math.floor(s / min11);
                taken = taken + Math.floor(s / min11); 
                s = s - (taken * min11);
                //Console Update
                console.log("Call Length: " + minutes + " mins" + " |" + " Cents Left: " + s + " ¢");
            }
        }
    }
    
    //Returns the minutes
    return minutes;
}