<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Michael Adeleke Olomola</title>
    <description>The latest articles on Forem by Michael Adeleke Olomola (@mikeadelek).</description>
    <link>https://forem.com/mikeadelek</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1097128%2Fcc2e2c3c-49e6-45ca-b835-7bee7d5659ce.jpg</url>
      <title>Forem: Michael Adeleke Olomola</title>
      <link>https://forem.com/mikeadelek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mikeadelek"/>
    <language>en</language>
    <item>
      <title>Rest APIs with the Weather App JavaScript Project.</title>
      <dc:creator>Michael Adeleke Olomola</dc:creator>
      <pubDate>Fri, 05 Apr 2024 23:26:26 +0000</pubDate>
      <link>https://forem.com/mikeadelek/rest-apis-with-the-javascript-project-c1</link>
      <guid>https://forem.com/mikeadelek/rest-apis-with-the-javascript-project-c1</guid>
      <description>&lt;p&gt;What next after learning the basics of Javascript? Well,  stay with me; I have got you covered. Ever heard of REST APIs? It's a powerful tool you have to add to your skills as a JS developer.&lt;/p&gt;

&lt;p&gt;This article is about REST APIs and how to use them as a JavaScript developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Content
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Who is this article for?&lt;/li&gt;
&lt;li&gt;Requirements?&lt;/li&gt;
&lt;li&gt;What is an API?
&lt;/li&gt;
&lt;li&gt;REST APIs?&lt;/li&gt;
&lt;li&gt;How to Make Requests with Rest APIs&lt;/li&gt;
&lt;li&gt;HTTP Methods&lt;/li&gt;
&lt;li&gt;GET HTTP Method&lt;/li&gt;
&lt;li&gt;POST HTTP Method&lt;/li&gt;
&lt;li&gt;PUT HTTP Method&lt;/li&gt;
&lt;li&gt;DELETE HTTP Method&lt;/li&gt;
&lt;li&gt;Practical Example: Weather Web Application&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Who is this article for?
&lt;/h2&gt;

&lt;p&gt;This article is tailor-made for JavaScript developers who are eager to take a step- further as a JavaScript developer. If you’re new to the concept of REST APIs or not sure about how they work, this is also for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Requirements?
&lt;/h2&gt;

&lt;p&gt;Basic knowledge of JavaScript is enough to get you started. Sounds good?&lt;br&gt;
To get started, let’s get acquainted with some definitions.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. What is an API?
&lt;/h2&gt;

&lt;p&gt;Think of API as the waiter that gets your order in a restaurant; the waiter serves as a bridge between you, the customer (who places an order), and the chef in the kitchen (who makes the food). The waiter acts as the API (Application Programming Interface), which is a communication channel between two applications. For example, a web form submits data to a database.&lt;/p&gt;

&lt;p&gt;An API handles incoming web form requests  and sends a response back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ggkf58c0hqhxyns5upx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ggkf58c0hqhxyns5upx.png" alt="Image description" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  4. REST APIs?
&lt;/h2&gt;

&lt;p&gt;REST (Representational State Transfer) is a type of API that makes communication between two applications smooth and organized by following a set of rules.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. How to Make Requests with Rest APIs
&lt;/h2&gt;

&lt;p&gt;Let’s explore the following example, there is a common Rest API called jsonplaceholder which provides random user data. Using JavaScript fetch() API or async/await, let’s make a request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch("https://jsonplaceholder.typicode.com/users")
    .then(response =&amp;gt; response.json())
    .then(json =&amp;gt; console.log(json))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can use the async / await method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function GetData() {
    let endPoint = "https://jsonplaceholder.typicode.com/users";
    const request = await fetch(endPoint);
    const response = await request.json();
    console.log(response)
}
GetData() // call the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check out this link to a beginner’s guide to Promise in JavaScript. If you’re not used to this code, &lt;strong&gt;&lt;em&gt;&lt;a href="https://www.freecodecamp.org/news/javascript-promises-for-beginners/"&gt;https://www.freecodecamp.org/news/javascript-promises-for-beginners/&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
So, if you want to add data to some kind of database using an API, you are going to use the fetch API. However, this time, you will have to specify the methods you want to use.&lt;br&gt;
Let’s explore some of those methods.&lt;/p&gt;
&lt;h2&gt;
  
  
  6. HTTP Methods
&lt;/h2&gt;

&lt;p&gt;HTTP methods let the REST API know the type of request you want to make. The most common types are GET, DELETE, POST, and PUT, all known as CRUD operations (Create, Read, Update, Delete).&lt;br&gt;
Let us explore the methods.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. GET HTTP Method
&lt;/h2&gt;

&lt;p&gt;This method is used to fetch information from the server. For a get request, the server searches for the data you requested and sends it back to you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function userData() {
    let endPoint = "https://jsonplaceholder.typicode.com/users";
    const request = await fetch(endPoint, {method: "GET"});
    const response = await request.json();
    console.log(response)
}
userData() // call the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. POST HTTP Method
&lt;/h2&gt;

&lt;p&gt;POST Method is used to add data to REST API. body option is to pass the data to our API. However, this data is usually stored in JSON format, so we convert to the object to JSON with the JSON.stringify() method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function addData() {
  let endPoint = "https://jsonplaceholder.typicode.com/users";
  const request = await fetch(endPoint, {
    method: "POST",
    body: JSON.stringify(data)
  });
  const response = await request.json();
  console.log(response);
}
const data = { firstName: "Jane Doe", age: 25 };

addData(); // call the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. PUT HTTP Method
&lt;/h2&gt;

&lt;p&gt;This method update the data when working with REST API.&lt;br&gt;
Let’s update user id 3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function UpdateData() {
  let endPoint = "https://jsonplaceholder.typicode.com/users/3";
  const request = await fetch(endPoint, {
    method: "PUT",
    body: JSON.stringify(data)
  });
  const response = await request.json();
  console.log(response);
}
const data = { age: 32 };

UpdateData(); // call the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. DELETE HTTP Method
&lt;/h2&gt;

&lt;p&gt;This method allows you to delete user data from the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function deleteData() {
  let endPoint = "https://jsonplaceholder.typicode.com/users/10";
  const request = await fetch(endPoint, { method: "DELETE" });
  const response = await request.json();
  console.log(response);
}

deleteData(); // call the function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  11. Practical Example: Weather Web Application
&lt;/h2&gt;

&lt;p&gt;Let’s build a weather web application. To get this started, we are going to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To get the API key, you need to sign up with &lt;a href="https://openweathermap.org/api"&gt;https://openweathermap.org/api&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s build the HTML and CSS of the web application. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create HTML file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;div class="input"&amp;gt;
        &amp;lt;input type="text" class="inputValue" placeholder="Enter Location" /&amp;gt;
        &amp;lt;button class="button"&amp;gt;&amp;lt;i class="fas fa-search"&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class="displayWeather"&amp;gt;
        &amp;lt;h1 class="temp"&amp;gt;----&amp;amp;deg;C&amp;lt;/h1&amp;gt;
        &amp;lt;p class="desc"&amp;gt;&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create CSS file &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*click this link to access the css file... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create JS file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 1: To get the API key, you need to sign up with &lt;a href="https://openweathermap.org/api"&gt;https://openweathermap.org/api&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;const apiKey = "YOUR_API_KEY";&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;const apiUrl = "&lt;a href="https://api.openweathermap.org/data/2.5/weather"&gt;https://api.openweathermap.org/data/2.5/weather&lt;/a&gt;";&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 2: Create variable for all HTML elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create variable for all HTML content
let button = document.querySelector(".button");
let inputvalue = document.querySelector(".inputValue");
let nameVal = document.querySelector(".name");
let temp = document.querySelector(".temp");
let desc = document.querySelector(".desc");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3:&lt;br&gt;
Add EventListener to the search button to fetch weather data for the entered city and display an alert message if you enter the wrong city.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Add EventListener to the search button
button.addEventListener("click", function () {
  // Fetch data from open weather API
  fetch(
    `https://api.openweathermap.org/data/2.5/weather?q=${inputvalue.value}&amp;amp;units=metric&amp;amp;appid=108dd9a67c96f23039937fe6f3c91963`
  )
    .then((response) =&amp;gt; response.json())
    .then(displayData)
    .catch((err) =&amp;gt; alert("Wrong City name"));
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 4:&lt;br&gt;
Create a displayData function to display the weather on the HTML element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// create Function to display weather on html document
const displayData = (weather) =&amp;gt; {
  temp.innerText = `${weather.main.temp}°C`;
  desc.innerText = `${weather.weather[0].main}`;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding REST APIs is so important for a JavaScript developer. This article has helped sharpen your understanding of REST APIs and how you can use them to build your own applications. &lt;/p&gt;

&lt;p&gt;The weather app was gotten from medium Alwazkazi3&lt;br&gt;
&lt;a href="https://alwazkazi3.medium.com/creating-a-weather-app-using-api-javascript-4d7bb26bbc92"&gt;https://alwazkazi3.medium.com/creating-a-weather-app-using-api-javascript-4d7bb26bbc92&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>frontend</category>
    </item>
    <item>
      <title>JavaScript Array Map()</title>
      <dc:creator>Michael Adeleke Olomola</dc:creator>
      <pubDate>Wed, 13 Mar 2024 16:56:42 +0000</pubDate>
      <link>https://forem.com/mikeadelek/javascript-array-map-574h</link>
      <guid>https://forem.com/mikeadelek/javascript-array-map-574h</guid>
      <description>&lt;p&gt;Map() is an array method that applies a function we provide to each element of an array and creates a new array with the result.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const products = ['Burger', 'Beef’, 'Pizza', 'Chicken']
const mappedProducts = products.map (product = '${product}*')

console.log(mappedProducts) 

// Output: ['Burger *', 'Beef *', 'Pizza *', 'Chicken *']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Map() method takes an array of elements and returns a new array after transforming the element based on a function we supplied.&lt;/p&gt;

&lt;p&gt;In simple English, this line of code is:&lt;br&gt;
Given an array called products, take each element in the array and add an asterisk to it. Then return a new array with all the transformed elements. When finished, store the new array in a variable called'mappedProducts'.&lt;/p&gt;

&lt;p&gt;How to map an array without the map() method&lt;br&gt;
To map an array without using the map() method, you can use a for loop to iterate through each element of the array.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const number = [2, 4, 6, 8, 10]
const mappedNumbers = [ ];

for (let i = 0; i &amp;lt; numbers.length; i++) {
   mappedNumbers.push(numbers[ i ] * 2);
}

console.log(mappedNumbers);

// output: [4, 8, 12, 16, 20]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example shows the number of arrays [2, 4, 6, 8, 10].&lt;/p&gt;

&lt;p&gt;You create an empty array called mappedNumbers to store the mapped values. Use a for loop to iterate through each element of the number array and multiply by 2. &lt;/p&gt;

&lt;p&gt;Push the result value into the mappedNumbers array. Finally, log the mappedNumbers array to the console, which gives you the mapped array [4, 8, 12, 16, 20]. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
