I'm super new to programming and everything, and currently I'm joining this course by Google. So there is one exercise that I have to compare the time I've spent on using my phone yesterday and today. Below is the code that I've written. I need the output to be true
, false
, false
but my output was true
, false
, true
. I've tried to solve but it just turned worse. I need help!
fun main() {
var timeSpentToday = 300
var timeSpentYesterday = 250
println(timeSpent(timeSpentToday, timeSpentYesterday))
println()
println(timeSpent(300, 300))
println()
println(timeSpent(200, 220))
println()
}
fun timeSpent(today: Int, yesterday: Int): Boolean {
val today = 300
val yeseterday = 250
return today > yesterday
}
I tried to change the input value and it works if I change the penultimate println
to println(timeSpent(200,300)
. However, it doesn't work if the number is in range of 200 to 290
CodePudding user response:
fun timeSpent(today: Int, yesterday: Int): Boolean {
val today = 300
val yeseterday = 250
return today > yesterday
}
Well, you fully ignore the value of today that is passed into timeSpent, replacing it with a new variable that always has the value 300. You also try to do that with yesterday
, but a typo actually means you do the right thing. In any event, timeSpent should have two of its three lines deleted:
fun timeSpent(today: Int, yesterday: Int): Boolean {
return today > yesterday
}