항해 프로그래머스

항해3주차 자바스크립트 프로그래머스 - 옷가게 할인받기

완두노예 2023. 1. 27. 11:52

나의 풀이

parseInt & Math.floor

function solution(price) {
    if(price >= 500000) {
        price *= 0.8;
    } else if(price >= 300000) {
        price *= 0.9;
    } else if(price >= 100000) {
        price *= 0.95;
    }
    return Math.floor(price);
}
// if 10만원>= price*0.05//아니지 price*0.95
//else if 30만원>= price*0.1//아니지 price*0.9
//else if 50만원>= price*0.2//아니지 price*0.8
//return parseint(answer)

다른 풀이

function solution(price) {
    var answer = price;
    if (answer >= 500000) {
        answer *= 0.8;
    } else if (answer >= 300000) {
        answer *= 0.9;
    } else if (answer >= 100000) {
        answer *= 0.95;
    }

    return parseInt(answer);
}
function solution(price) {
    if (price >= 500000)
        return parseInt(price*(1-0.2));

    if (price >= 300000)
        return parseInt(price*(1-0.1));

    if (price >= 100000)
        return parseInt(price*(1-0.05));  

    return price
}
const discounts = [
    [500000, 20],
    [300000, 10],
    [100000,  5],
]

const solution = (price) => {
    for (const discount of discounts)
        if (price >= discount[0])
            return Math.floor(price - price * discount[1] / 100)
    return price
}
function solution(price) {

    let arr_discount = [[500000, 0.2], [300000, 0.1], [100000, 0.05]]

    for(let i = 0 ; i < arr_discount.length ; i++)
        if(arr_discount[i][0] <= price) return Math.trunc(price * (1-arr_discount[i][1]))        

    return price
}

double tilde 연산자, Meth.floor === ~~

function solution(price) {
    price = price>=500000?price*0.8:price>=300000?price*0.9:price>=100000?price*0.95:price;
    return ~~(price);
}
function solution(price) {
    if (price >= 100000 && price < 300000) {
        return Math.floor(price * .95)
    } else if (price >= 300000 && price < 500000) {
        return Math.floor(price * .9)
    } else if (price >= 500000) {
        return Math.floor(price * .8)
    } else {
        return price
    }
}
function solution(price) {
  return price >= 500000
    ? Math.floor(price * (1 - 0.2))
    : price >= 300000
    ? Math.floor(price * (1 - 0.1))
    : price >= 100000
    ? Math.floor(price * (1 - 0.05))
    : price;
}
function solution(price) {
    const discount = price < 100000 ? 0 : price < 300000 ? price / 20 : price < 500000 ? price / 10 : price / 5;
    return Math.floor(price - discount);
}
const solution=(p)=>~~(p*[1, 0.95, 0.95, 0.9, 0.9, 0.8, 0.8, 0.8, 0.8, 0.8, 0.8][~~(p/100000)])
function solution(price) {
    return price >= 500000 ? Math.floor(price * 0.8) 
        : price >= 300000 ? Math.floor(price * 0.9) 
        : price >= 100000 ? Math.floor(price * 0.95) : Math.floor(price * 1)
}
function solution(p) {
    return ~~(p*(p>=5e5?0.8:p>=3e5?0.9:p>=1e5?0.95:1))
}