+ 1

Why does the code not work?

https://code.sololearn.com/ca17a1152A17 This is the code, it's in kotlin and from the course question of functions module test I tried quite a few things but it just gives the output as None Question: You are working on a eCommerce website and need to make a shipping cost calculator based on the order amount. The store uses the following cost structure: For orders in the US: - $75+ orders have free shipping - orders less than $75 have a shipping fee of 10% of the total order amount. For international orders, there is a 15% shipping fee, with a maximum of $50. This means that the maximum shipping fee for an international order is $50. You need to complete the given shippingCost() function, which takes the order amount and a Boolean indicating whether the order is international or not, and returns the shipping cost for that order. The return amount should be a Double.

26th Feb 2021, 10:10 AM
AquaRose
AquaRose - avatar
13 ответов
+ 14
fun shippingCost(amount: Double, international: Boolean): Double { if (international == false) { if (amount >= 75.0) { return 0.0 } else { return 0.1*amount } } else { var tax = 0.15*amount if (tax > 50.0) { tax = 50.0 } return tax } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
24th Mar 2021, 3:02 PM
Melhem Kheireddine
Melhem Kheireddine - avatar
+ 3
you doesn't return anthing from your function ^^ also, you could avoid second test in else statements, as your first if test cover the only other alternative (if x != false, then x == true, if not a <= b, then b < a...) fun shippingCost(total: Double, international: Boolean): Double { if (international == false) { if (total >= 75.0) { return 0.0 } else { return 0.1*total } } else { var tax = 0.15*total if (tax > 50.0) { tax = 50.0 } return tax } }
26th Feb 2021, 9:43 PM
visph
visph - avatar
+ 2
fun shippingCost(total: Double, international: Boolean): Double { if (international == false) { if (total >= 75.0) { return total*0.0 } else if (total < 75.0) { return 0.10*total } } else if (international == true) { if ((total*0.15) > 50.0) { return 50.0 } else{ return 0.15*total } } return 0.0 } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
7th May 2021, 3:19 PM
SAIKIRAN KOYYEDA
SAIKIRAN KOYYEDA - avatar
+ 1
fun shippingCost(total: Double, international: Boolean): Double { if (international == false) { if (total >= 75.0) { return total*0.0 } else if (total < 75.0) { return 0.10*total } } else if (international == true) { if ((total*0.15) > 50.0) { return 50.0 } else{ return 0.15*total } } return 0.0 } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) } try this
22nd Jul 2021, 2:00 PM
Ankit Chauhan
Ankit Chauhan - avatar
+ 1
//Have a look at this code -> fun shippingCost(amount: Double, international: Boolean): Double { val amount:Double = when(international){ false -> { if(amount>=75.0) 0.0 else (amount*0.1) } true -> { if((amount*0.15)>50.0) 50.0 else (amount*0.15) } } return amount } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
27th Jul 2021, 2:18 PM
Darshan Pagar
Darshan Pagar - avatar
0
please help me to do shipping calculator
2nd Mar 2021, 2:51 PM
Muhammadaziz Ravshanbekov
Muhammadaziz Ravshanbekov - avatar
0
Please help us to code for this problem 🙏
10th Mar 2021, 3:25 AM
Mukesh Kumar
Mukesh Kumar - avatar
0
Melhem Kheireddine I don't know if your solution is valid, but: if it is, that would help to pass the task by copy-pasting, but wouldn't help to learn... else, you must post your own question thread to be helped ^^
24th Mar 2021, 3:06 PM
visph
visph - avatar
0
fun shippingCost(amount: Double, international: Boolean): Double { var shipcost:Double if (international == false) { shipcost = 0.0 if (amount < 75.0) { shipcost = amount*0.1 } } else { shipcost = amount*0.15 if (shipcost > 50.0) { shipcost = 50.0 } } return shipcost } fun main() { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
4th Aug 2021, 7:44 PM
Sanprog-2008
Sanprog-2008 - avatar
0
Here is the solution: fun shippingCost(amount: Double, international: Boolean): Double { if (international == false) { if (amount >= 75.0) { return 0.0 } else { return 0.1*amount } } else { var tax = 0.15*amount if (tax > 50.0) { tax = 50.0 } return tax } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
25th Aug 2021, 2:52 PM
Owethu Sotomela
Owethu Sotomela - avatar
0
fun shippingCost(amount: Double, international: Boolean): Double { when { international == true && amount * 0.15 <= 50.0 -> return amount * 0.15 international == true && amount * 0.15 > 50.0 -> return 50.0 international != true && amount <= 75.0 -> return amount * 0.10 else -> return 0.0 } } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
5th Sep 2021, 4:53 PM
Iliodor
Iliodor - avatar
0
val amount:Double = when(international){ false -> { if(amount>=75.0) 0.0 else (amount*0.1) } true -> { if((amount*0.15)>50.0) 50.0 else (amount*0.15) } } return amount
22nd Oct 2021, 10:00 PM
Alireza Nasrollahzadeh
Alireza Nasrollahzadeh - avatar
0
fun shippingCost(amount: Double, international: Boolean): Double { var cost = 0.0; if(international){ cost = amount * 0.15 if(cost > 50.0){ cost = 50.0 } } else{ if(amount > 75){ cost = 0.0 } else if(amount < 75){ cost = amount*0.10 } } return cost; } fun main(args: Array<String>) { val total = readLine()!!.toDouble() val international = readLine()!!.toBoolean() println(shippingCost(total, international)) }
24th Feb 2022, 8:29 AM
Md. Al-Amin
Md. Al-Amin - avatar