Jump to content

simple code challenge


yomama

Recommended Posts

 

In the editor, I’ve declared a struct named RGBColor that models a color object in the RGB space.


Your task is to write a custom initializer method for the object. Using the initializer assign values to the first four properties. Using the values assigned to those properties create a value for the description property that is a string representation of the color object.


For example, given the values 86.0 for red, 191.0 for green, 131.0 for blue and 1.0 for alpha, each of the stored properties should hold these values and the description property should look like this:


"red: 86.0, green: 191.0, blue: 131.0, alpha: 1.0"


Note: Init methods typically list parameters in the same order of property declaration. For this task, stick to the order red,green,blue,alpha.

 

 

 

struct RGBColor {
    let red: Double
    let green: Double
    let blue: Double
    let alpha: Double
    
    let description: String
  
    // Add your code below
    
    
    
}

Link to comment
Share on other sites

init(red:decimal, green : decimal, blue: decimal, alpha : decimal){

self.red = red

self.blue = blue

self.green = green

self.alpha = alpha

}

// you can create a function to display the description which concatenates the string or also directly define that values in the struct too.

description = "red : \(red), green: \(green), blue : \(blue), alpha : \(alpha)" -> (change the string interpolation to ToString also if needed from decimal and take away the 0's) 

Link to comment
Share on other sites

8 minutes ago, loveindia said:

init(red:decimal, green : decimal, blue: decimal, alpha : decimal){

self.red = red

self.blue = blue

self.green = green

self.alpha = alpha

}

// you can create a function to display the description which concatenates the string or also directly define that values in the struct too.

description = "red : \(red), green: \(green), blue : \(blue), alpha : \(alpha)" -> (change the string interpolation to ToString also if needed from decimal and take away the 0's) 

ragada11.gif uncle almost correct kani , struct lo double ani declare cheste nuv decimal ani init method parameters passing. description kuda init lo calculate chey. 

init(red: Double, green: Double, blue: Double, alpha: Double) {
    self.red = red
    self.green = green
    self.blue = blue
    self.alpha = alpha
    description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}
Link to comment
Share on other sites

2 hours ago, yomama said:

ragada11.gif uncle almost correct kani , struct lo double ani declare cheste nuv decimal ani init method parameters passing. description kuda init lo calculate chey. 


init(red: Double, green: Double, blue: Double, alpha: Double) {
    self.red = red
    self.green = green
    self.blue = blue
    self.alpha = alpha
    description = "red: \(red), green: \(green), blue: \(blue), alpha: \(alpha)"
}

swift lo decimal ledu man... C# lo decimal undi... rendu kalipi code raasa man... ilanti chinna chinna vi choosi chudanattu vadileyaali man...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...