Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to convert

Can somebody break down how you convert the seconds into hours, minutes and seconds... I divided the seconds by sixty than divide that number by sixty to get the hours(664) but I'm lost on how I convert the rest can somebody please help me out

total seconds is 2390724

6 Answers

Matthew Long
Matthew Long
28,407 Points

What are you asking exactly? How to go get the decimal hours into minutes and seconds?

You have 664.09 hours equal to 2390724 seconds already. So set the 664 hours aside. Multiply .09 hours times 60 minutes to get 5.4 minutes. So now you have your minutes. Set aside the whole minutes. Multiple .4 minutes * 60 seconds. This gives you 24 seconds. This means 2390724 seconds equals 664 hours 05 minutes and 24 seconds.

If that's not what you're asking for, I need more to go on.

Thanks, Matthew that's what I was looking for. I didn't know that what I had to do bro!

Formatting Time

You have been given a time value measured in seconds. Your task is to define a string literal that describes the time in hours, minutes and seconds. To achieve this, you will need to convert from the value in seconds to hours, minutes and seconds. An example of the final result we're looking for:

9700 seconds is 2 hours, 41 minutes and 40 seconds */

let time = 2390724

let hours = time/3600 (664hours) let minutes = (time % 3600)/60 (5 minutes) let seconds = time % 60 (24 seconds) "(time) seconds is (hours) hours,(minutes) minutes and (seconds) seconds"

I have a few questions that I need clarification on

  1. why do we put (time % 3600) in quotations? and why do we divide the that by 60? I thought we multiple that (I'm lost lol)
  2. why do we do time % 60?
  3. I don't understand how they wrote this string. please explain it to me
    PLEASE HELP!!
Matthew Long
Matthew Long
28,407 Points

Questions 1:

I'm not sure what you mean by quotations? Do you mean parentheses? If so it's in parentheses because that operation needs to occur first. I believe it would be this way regardless, but this way it's easier to read. Think of the mod operator, % as remainder. 2390724%3600 will give you the remainder of 2390724/3600 in seconds. This is why you divide instead of multiply like in my answer above. You are going from seconds to minutes instead of hours to minutes. Pay attention to your units.

Questions 2:

2390724%60 will give you the remainder of the 2390724/60 in seconds.

Question 3:

Looks like you need to include the backslash for your string to recognize your variables instead of simply a string:

"\(time) seconds is \(hours) hours, \(minutes) minutes and \(seconds) seconds."

question 3: why is it (hours) hours, (minutes) minutes and (seconds) seconds that's the part I'm lost on why does it repeat itself? I'm lost and thank you for clarifying that for me Matthew

why don't it just read "(time) seconds is (hours) (minutes) and (seconds)"

Matthew Long
Matthew Long
28,407 Points

The part in the parenthese is a variable. You're missing the backslash in front of the parenthese. It will be rendered as the variable value.

I apologize the code was wrong "(time) seconds is (hours) hours, (minutes) minutes and (seconds) seconds." how did you know to write this? that's the part I'm lost on

Matthew Long
Matthew Long
28,407 Points

Because string interpolation is the simplest way to get the output you're aiming for. You could have also written it using string concatenation:

let timeConstant = 2390724
let hourConstant = timeConstant / 3600 
let minuteConstant = (timeConstant % 3600) / 60
let secondConstant  = timeConstant % 60

// string concatenation 
timeConstant + " seconds is " + hourConstant + " hours, " + minuteConstant + " minutes and " + secondConstant + " seconds."

// string interpolation 
"\(timeConstant) seconds is \(hourConstant) hours, \(minuteConstant) minutes and \(secondConstant) seconds."

// both methods output
"2390724 seconds is 664 hours, 5 minutes and 24 seconds."

I changed the constant names to maybe clear up some confusion.

Thank you for all your help. Can you help me understand this part now?

The code provided below defines the location of a tower and an enemy on a two dimensional coordinate map. towerX and towerY are the X and Y positions of the Tower while enemyX and enemyY are the X and Y values of the enemy. */

let towerX = 2.0 let towerY = 13.0

let enemyX = 4.0 let enemyY = 9.0

/*: Write code to determine how far they are from one another using the Distance Formula. The Distance Formula can be broken down into the following steps

  • Subtract the distance between the X coordinates of the Tower and Enemy and square it
  • Subtract the distance between the Y coordinates of the Tower and Enemy and square it
  • Add the two square values
  • Obtain the square root of the resulting value */

// Enter your code here let distancex = enemyX - towerX

//: For the final part, obtaining the square root, replace the 1 inside of the sqrt(1) with the value you want to compute the square root for.

import Foundation

let root = sqrt(1)