Two brands sell the same product. Show the one with the higher rating.
You start with:
const brand1: string = "TechPro";
const rating1: number = 4;
const brand2: string = "MegaCorp";
const rating2: number = 5;
let bestBrand: string;
let bestRating: number;
Build:
1. MegaCorp (5)
Rules:
- Use
if/elseto pick the winner:brand1whenrating1 > rating2, otherwisebrand2. - The starter already declares
bestBrandandbestRating: assign the winner's name and rating to them inside the branches. - Format the result as
"1. Brand (rating)"and print it withconsole.log(...).
Pick the winner into
bestBrand and bestRating first, then build the output text from them.
TypeScript turns the number into text for you when you join it to a string with
+.
const brand1: string = "TechPro";
const rating1: number = 4;
const brand2: string = "MegaCorp";
const rating2: number = 5;
let bestBrand: string;
let bestRating: number;
if (rating1 > rating2) {
bestBrand = brand1;
bestRating = rating1;
} else {
bestBrand = brand2;
bestRating = rating2;
}
console.log("1. " + bestBrand + " (" + bestRating + ")");
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
