How can i do this math in function in javascript?

this is the clue, if you walk 10 ft you have to pay 20 dollar, but if you walk 11 to 20 ft you have to pay 18 dollar and if you walk for than 20 ft you have to pay 15 dollar.

what have you tried so far?

So on a conceptual level, sounds like you want to create a function that takes in the amount of feet walked and based on that returns to correct dollar value using conditional logic.

i am a newbee. and i cant figure out the funcional way to do so. Please give me an example

i tried with function with if else statement

how did it go? can you show what you tried?

Yeah just post what you tried and we can try to help you figure it out.

var it = 10

if (x<=10){

var y = x*10

}else if (x<=20 ){

}else if (x>21){

y = (x-


ok if I was doing this, I would start with something like

function tollByFeet(feet){
//put your if checks and returns here
//now you can call it with different values
1 Like

How about this? I didn’t know what it should do if you walk less than 10 feet so I wrote that last else statement. Now I should figure out why feet=10 doesn’t work, but feet===10 does.

function howMuchToPay(feet) {
    if (feet===10) {
    } else if (feet>=11 && feet<=20) {
    } else if (feet>20) {
    } else {
        console.log("Not a valid number of feet");

this is an assignment, you are adding 10 to the feet variable

comparisons are == or ===

Thank you. I’m slowly starting to get this.