Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 77 additions & 3 deletions task-1/cocktail.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,77 @@
// API documentation: https://www.thecocktaildb.com/api.php

import path from 'path';
import path from "path";
import fsPromises from "node:fs/promises";

const BASE_URL = 'https://www.thecocktaildb.com/api/json/v1/1';
const BASE_URL = "https://www.thecocktaildb.com/api/json/v1/1";

// Add helper functions as needed here

function getAlcoString(strAlcoholic) {
if (strAlcoholic === "Alcoholic") {
return "Yes";
} else {
return "No";
}
}

function generateMarkdown(data) {
const drinks = data.drinks;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If drinks returned null , the function will still try to for...of over null, which will throw a TypeError.

let markdown = "# Cocktail Recipes";
for (const drink of drinks) {
markdown += generateDrinkMarkdown(drink);
}
return markdown;
}

function getIngredients(drink) {
let ingredients = "";

for (let i = 1; i <= 15; i++) {
const ingredient = drink["strIngredient" + i];
const measure = (drink["strMeasure" + i] ?? "").trim();

if (!ingredient) break;
if (measure) {
ingredients += `- ${measure} ${ingredient}\n`;
} else {
ingredients += `- ${ingredient}\n`;
}
}

return ingredients;
}

function generateDrinkMarkdown(drink) {
return `

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These empty lines are visible in the markdown


## ${drink.strDrink}

![${drink.strDrink}](${drink.strDrinkThumb}/medium)

**Category**: ${drink.strCategory}

**Alcoholic**: ${getAlcoString(drink.strAlcoholic)}

### Ingredients

${getIngredients(drink)}


### Instructions

${drink.strInstructions}

Serve in: ${drink.strGlass}


`;
}

export async function main() {
if (process.argv.length < 3) {
console.error('Please provide a cocktail name as a command line argument.');
console.error("Please provide a cocktail name as a command line argument.");
return;
}

Expand All @@ -20,10 +83,21 @@ export async function main() {

try {
// 1. Fetch data from the API at the given URL

const responce = await fetch(url);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response not responce

if (!responce.ok) {
throw new Error("not ok" + responce.status);
}
const data = await responce.json();
console.log(JSON.stringify(data.drinks[0], null, 2));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug console.logs should be deleted from the file when the development is finished.

// 2. Generate markdown content to match the examples
const str = generateMarkdown(data);

// 3. Write the generated content to a markdown file as given by outPath
await fsPromises.writeFile(outPath, str);
} catch (error) {
// 4. Handle errors
console.error("No cocktails found with that name.");
}
}

Expand Down
27 changes: 27 additions & 0 deletions task-1/output/margarita.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Cocktail Recipes


## Margarita

![Margarita](https://www.thecocktaildb.com/images/media/drink/5noda61589575158.jpg/medium)

**Category**: Ordinary Drink

**Alcoholic**: Yes

### Ingredients

- 1 1/2 oz Tequila
- 1/2 oz Triple sec
- 1 oz Lime juice
- Salt



### Instructions

Rub the rim of the glass.

Serve in: Cocktail glass


Loading