CodeBlanks Logo Icon CodeBlanks

Display

Write the function that formats the weather.

What is a display function?

A display function converts weather data into a string to show the user. It combines city, temperature, and condition information with the + operator. This keeps model data separate from the user interface text.

function format(h: Weather): string {
  return h.city + " " + h.temperature + "C";
}

String concatenation

During formatting, text pieces are combined with the + operator. Numeric values are automatically converted to strings. For example, "Bursa" + " " + 20 + "C" gives "Bursa 20C". This method produces a simple and readable output.

Practice formatla function

Define a Hava interface: sehir: string, sicaklik: number. The formatla function should be (h: Hava): string and return h.sehir + " " + h.sicaklik + "C". Print the result of calling formatla({ sehir: "Bursa", sicaklik: 20 }) with console.log.

Starter code

index.ts
// Hava interface tanımla: sehir, sicaklik
// formatla fonksiyonu string dönsün
// sehir + " " + sicaklik + "C" birleştir
// formatla çağırıp yazdır

Sample solution

index.ts
interface Hava { sehir: string; sicaklik: number; }
function formatla(h: Hava): string {
  return h.sehir + " " + h.sicaklik + "C";
}
console.log(formatla({ sehir: "Bursa", sicaklik: 20 }));

Expected output

Bursa 20C

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What type does the formatla function return?

  • string Correct answer
  • boolean
  • void
  • number

Explanation: Since the display function produces the text to show the user, it returns a string value.

2 String concatenation operator

Question
function formatla(h: Hava): string {
  return h.sehir + " " + h.sicaklik ____ "C";
}
Answer
function formatla(h: Hava): string {
  return h.sehir + " " + h.sicaklik + "C";
}

Explanation: The + operator is used to concatenate strings; numeric values are automatically converted to strings.

3 Display concepts

Term Meaning
formatla Formatting function
string Return type
+ String concatenation
Hava Input type

All lessons in Intermediate