rodriggj / Go

0 stars 0 forks source link

1.9 Excercises #9

Open rodriggj opened 2 years ago

rodriggj commented 2 years ago

Exercise 1: make it blue

  1. Change color variable's value to "blue"
  2. Print it EXPECTED OUTPUT: blue

Process:

  1. Create the directory structure
    mkdir 1_makeItBlue
    cd 1_makeItBlue
  2. Initiate the go project
    go mod init main
  3. Create the main.go file for housing the code
    code main.go
  4. Code the logic
    
    package main

import "fmt"

func main() { var color string = "green" color = "blue"

fmt.Println("color:", color)

}

5. Run the program
```sh
go run main.go
rodriggj commented 2 years ago

Exercise 2

Task

  1. Change the value of color variable to "dark green"
  2. Do not assign "dark green" to color directly Instead, use the color variable again while assigning to color
  3. Print it

RESTRICTIONS WRONG ANSWER, DO NOT DO THIS: color = "dark green"

HINT


Process

Repeated steps above, with this logic

package main

import (
    "fmt"
)

func main() {
    color := "green"
    nc := "dark"

    fmt.Println("New Color:", nc+" "+color)
}
rodriggj commented 2 years ago

Excercise 3: Assign with Expressions

Task

  1. Multiply 3.14 with 2 and assign it to n variable
  2. Print the n variable

HINT Example: 3 * 2 = 6


Repeated same process with the following logic

package main

import (
    "fmt"
)

func main() {
    n := 3.14 * 2
    fmt.Println("3.14 * 2 = ", n)
}
rodriggj commented 2 years ago

Exercise 4: Find the Perimeter

// EXERCISE: Find the Rectangle's Perimeter // // 1. Find the perimeter of a rectangle // Its width is 5 // Its height is 6 // // 2. Assign the result to the perimeter variable // // 3. Print the perimeter variable // // HINT // Formula = 2 times the width and height // // EXPECTED OUTPUT // 22 // // BONUS // Find more formulas here and calculate them in new programs // https://www.mathsisfun.com/area.html // ---------------------------------------------------------

func main() { // UNCOMMENT THE CODE BELOW:

// var (
//  perimeter        int
//  width, height = 5, 6
// )

// USE THE VARIABLES ABOVE WHEN CALCULATING YOUR RESULT

// ADD YOUR CODE BELOW

}


Repeated the same process with the following logic

package main

import "fmt"

func main() {
    var (
        perimeter     int
        width, height = 5, 6
    )

    perimeter = 2 * (width + height)

    fmt.Println("Perimeter equals: ", perimeter)
}
rodriggj commented 2 years ago

Excercise 5: Multi Assign

// --------------------------------------------------------- // EXERCISE: Multi Assign // // 1. Assign "go" to lang variable // and assign 2 to version variable // using a multiple assignment statement // // 2. Print the variables // // EXPECTED OUTPUT // go version 2 // ---------------------------------------------------------

func main() { // DO NOT TOUCH THIS var ( lang string version int )

// ADD YOUR CODE BELOW

// DO NOT TOUCH THIS
fmt.Println(lang, "version", version)

}


Repeated same process with the following logic

package main

import "fmt"

func main() {
    // DO NOT TOUCH THIS
    var (
        lang    string
        version int
    )

    // ADD YOUR CODE BELOW
    lang = "go"
    version = 2

    // DO NOT TOUCH THIS
    fmt.Println(lang, "version", version)
}
rodriggj commented 2 years ago

Exercise 6: Multi-Assign 2

// --------------------------------------------------------- // EXERCISE: Multi Assign #2 // // 1. Assign the correct values to the variables // to match to the EXPECTED OUTPUT below // // 2. Print the variables // // HINT // Use multiple Println calls to print each sentence. // // EXPECTED OUTPUT // Air is good on Mars // It's true // It is 19.5 degrees // ---------------------------------------------------------


Repeated same process with the following logic

package main

import (
    "fmt"
)

func main() {
    var (
        planet string
        isTrue bool
        temp   float64
    )

    planet, isTrue, temp = "mars", true, 19.5

    fmt.Println("Air is good on", planet)
    fmt.Println("It's", isTrue)
    fmt.Println("It is", temp, "degrees")
}

NOTE: You don't need to add spacing (e.g "Air is good on ", planet -> "Air is good on", planet).