24 |
25 |
26 |
27 |
28 |
Do you accept all major credit cards?
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est
42 | dolore illo dolores quia nemo doloribus quaerat, magni numquam
43 | repellat reprehenderit.
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
do you suppport local farmers?
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est
66 | dolore illo dolores quia nemo doloribus quaerat, magni numquam
67 | repellat reprehenderit.
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
do you use organic ingredients?
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est
91 | dolore illo dolores quia nemo doloribus quaerat, magni numquam
92 | repellat reprehenderit.
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/Accordion/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/Accordion/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | ===============
3 | Fonts
4 | ===============
5 | */
6 | @import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap");
7 |
8 | /*
9 | ===============
10 | Variables
11 | ===============
12 | */
13 |
14 | :root {
15 | /* dark shades of primary color*/
16 | --clr-primary-1: hsl(205, 86%, 17%);
17 | --clr-primary-2: hsl(205, 77%, 27%);
18 | --clr-primary-3: hsl(205, 72%, 37%);
19 | --clr-primary-4: hsl(205, 63%, 48%);
20 | /* primary/main color */
21 | --clr-primary-5: #49a6e9;
22 | /* lighter shades of primary color */
23 | --clr-primary-6: hsl(205, 89%, 70%);
24 | --clr-primary-7: hsl(205, 90%, 76%);
25 | --clr-primary-8: hsl(205, 86%, 81%);
26 | --clr-primary-9: hsl(205, 90%, 88%);
27 | --clr-primary-10: hsl(205, 100%, 96%);
28 | /* darkest grey - used for headings */
29 | --clr-grey-1: hsl(209, 61%, 16%);
30 | --clr-grey-2: hsl(211, 39%, 23%);
31 | --clr-grey-3: hsl(209, 34%, 30%);
32 | --clr-grey-4: hsl(209, 28%, 39%);
33 | /* grey used for paragraphs */
34 | --clr-grey-5: hsl(210, 22%, 49%);
35 | --clr-grey-6: hsl(209, 23%, 60%);
36 | --clr-grey-7: hsl(211, 27%, 70%);
37 | --clr-grey-8: hsl(210, 31%, 80%);
38 | --clr-grey-9: hsl(212, 33%, 89%);
39 | --clr-grey-10: hsl(210, 36%, 96%);
40 | --clr-white: #fff;
41 | --clr-red-dark: hsl(360, 67%, 44%);
42 | --clr-red-light: hsl(360, 71%, 66%);
43 | --clr-green-dark: hsl(125, 67%, 44%);
44 | --clr-green-light: hsl(125, 71%, 66%);
45 | --clr-gold: #c59d5f;
46 | --clr-black: #222;
47 | --ff-primary: "Roboto", sans-serif;
48 | --ff-secondary: "Open Sans", sans-serif;
49 | --transition: all 0.3s linear;
50 | --spacing: 0.25rem;
51 | --radius: 0.5rem;
52 | --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
53 | --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
54 | --max-width: 1170px;
55 | --fixed-width: 620px;
56 | }
57 | /*
58 | ===============
59 | Global Styles
60 | ===============
61 | */
62 |
63 | *,
64 | ::after,
65 | ::before {
66 | margin: 0;
67 | padding: 0;
68 | box-sizing: border-box;
69 | }
70 | body {
71 | font-family: var(--ff-secondary);
72 | background: var(--clr-grey-10);
73 | color: var(--clr-grey-1);
74 | line-height: 1.5;
75 | font-size: 0.875rem;
76 | }
77 | ul {
78 | list-style-type: none;
79 | }
80 | a {
81 | text-decoration: none;
82 | }
83 | img:not(.logo) {
84 | width: 100%;
85 | }
86 | img {
87 | display: block;
88 | }
89 |
90 | h1,
91 | h2,
92 | h3,
93 | h4 {
94 | letter-spacing: var(--spacing);
95 | text-transform: capitalize;
96 | line-height: 1.25;
97 | margin-bottom: 0.75rem;
98 | font-family: var(--ff-primary);
99 | }
100 | h1 {
101 | font-size: 3rem;
102 | }
103 | h2 {
104 | font-size: 2rem;
105 | }
106 | h3 {
107 | font-size: 1.25rem;
108 | }
109 | h4 {
110 | font-size: 0.875rem;
111 | }
112 | p {
113 | margin-bottom: 1.25rem;
114 | color: var(--clr-grey-5);
115 | }
116 | @media screen and (min-width: 800px) {
117 | h1 {
118 | font-size: 4rem;
119 | }
120 | h2 {
121 | font-size: 2.5rem;
122 | }
123 | h3 {
124 | font-size: 1.75rem;
125 | }
126 | h4 {
127 | font-size: 1rem;
128 | }
129 | body {
130 | font-size: 1rem;
131 | }
132 | h1,
133 | h2,
134 | h3,
135 | h4 {
136 | line-height: 1;
137 | }
138 | }
139 | /* global classes */
140 |
141 | .btn {
142 | text-transform: uppercase;
143 | background: transparent;
144 | color: var(--clr-black);
145 | padding: 0.375rem 0.75rem;
146 | letter-spacing: var(--spacing);
147 | display: inline-block;
148 | transition: var(--transition);
149 | font-size: 0.875rem;
150 | border: 2px solid var(--clr-black);
151 | cursor: pointer;
152 | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
153 | border-radius: var(--radius);
154 | }
155 | .btn:hover {
156 | color: var(--clr-white);
157 | background: var(--clr-black);
158 | }
159 | /* section */
160 | .section {
161 | padding: 5rem 0;
162 | }
163 |
164 | .section-center {
165 | width: 90vw;
166 | margin: 0 auto;
167 | max-width: 1170px;
168 | }
169 | @media screen and (min-width: 992px) {
170 | .section-center {
171 | width: 95vw;
172 | }
173 | }
174 | main {
175 | min-height: 100vh;
176 | display: grid;
177 | place-items: center;
178 | }
179 | /*
180 | ===============
181 | Questions
182 | ===============
183 | */
184 | .title {
185 | margin-top: 15vh;
186 | margin-bottom: 4rem;
187 | }
188 | .title h2 {
189 | color: var(--clr-gold);
190 | font-family: "Great Vibes", cursive;
191 | text-align: center;
192 | }
193 | .section-center {
194 | max-width: var(--fixed-width);
195 | }
196 | .question {
197 | background: var(--clr-white);
198 | border-radius: var(--radius);
199 | box-shadow: var(--light-shadow);
200 | padding: 1.5rem 1.5rem 0 1.5rem;
201 | margin-bottom: 2rem;
202 | }
203 | .question-title {
204 | display: flex;
205 | justify-content: space-between;
206 | align-items: center;
207 | text-transform: capitalize;
208 | padding-bottom: 1rem;
209 | }
210 | .question-title p {
211 | margin-bottom: 0;
212 | letter-spacing: var(--spacing);
213 | color: var(--clr-grey-1);
214 | }
215 | .question-btn {
216 | font-size: 1.5rem;
217 | background: transparent;
218 | border-color: transparent;
219 | cursor: pointer;
220 | color: var(--clr-gold);
221 | transition: var(--transition);
222 | }
223 | .question-btn:hover {
224 | transform: rotate(90deg);
225 | }
226 | .question-text {
227 | padding: 1rem 0 1.5rem 0;
228 | border-top: 1px solid rgba(0, 0, 0, 0.2);
229 | }
230 | .question-text p {
231 | margin-bottom: 0;
232 | }
233 | /* hide text */
234 | /* .question-text {
235 | display: none;
236 | }
237 | .show-text .question-text {
238 | display: block;
239 | }
240 | .minus-icon {
241 | display: none;
242 | }
243 | .show-text .minus-icon {
244 | display: inline;
245 | }
246 | .show-text .plus-icon {
247 | display: none;
248 | } */
249 |
--------------------------------------------------------------------------------
/Array.from-Pagination/app.js:
--------------------------------------------------------------------------------
1 | import fetchFollowers from "./fetchFollowers.js";
2 | import displayFollowers from "./displayFollowers.js";
3 | import paginate from "./paginate.js";
4 | import displayButtons from "./displayButtons.js";
5 | const title = document.querySelector(".section-title h1");
6 | const btnContainer = document.querySelector(".btn-container");
7 | let index = 0;
8 | let pages = [];
9 |
10 | const setUpUI = () => {
11 | displayFollowers(pages[index]);
12 | displayButtons(btnContainer, pages, index);
13 | };
14 | const init = async () => {
15 | const followers = await fetchFollowers();
16 | title.textContent = "Pagination";
17 | pages = paginate(followers);
18 | setUpUI();
19 | };
20 | btnContainer.addEventListener('click',function(e){
21 | let style = e.target.classList.contains("page-btn");
22 | if(style){
23 | index = e.target.dataset.index;
24 | parseInt(index)
25 | }
26 | if (e.target.classList.contains("next-btn")) {
27 | index++
28 | if (index > pages.length-1) {
29 | index=0
30 | }
31 | }
32 | if (e.target.classList.contains("prev-btn")) {
33 | index--;
34 | if (index<0) {
35 | index = pages.length - 1;
36 | }
37 | }
38 | setUpUI()
39 | });
40 | window.addEventListener("load", init);
41 |
--------------------------------------------------------------------------------
/Array.from-Pagination/displayButtons.js:
--------------------------------------------------------------------------------
1 | const displayButtons = (container,pages,index) => {
2 | let buttons= pages?.map((_,pageIndex)=>{
3 | return `
${pageIndex + 1} `;
6 | })
7 |
8 | buttons.push('
next ')
9 | buttons.unshift('
prev ');
10 | container.innerHTML=buttons.join('')
11 | }
12 |
13 | export default displayButtons
14 |
--------------------------------------------------------------------------------
/Array.from-Pagination/displayFollowers.js:
--------------------------------------------------------------------------------
1 | const container = document.querySelector(".container");
2 | const display = (followers) => {
3 | const newpagination = followers
4 | ?.map((follower) => {
5 | const { avatar_url, login, html_url } = follower;
6 | return `
7 |
8 | ${login}
9 | view profile
10 | `;
11 | })
12 | .join("");
13 |
14 | container.innerHTML = newpagination;
15 | };
16 |
17 | export default display;
18 |
--------------------------------------------------------------------------------
/Array.from-Pagination/fetchFollowers.js:
--------------------------------------------------------------------------------
1 | const url = "https://api.github.com/users/bradtraversy/followers?per_page=1000";
2 |
3 | const fetchFollowers = async () => {
4 | const response = await fetch(url);
5 | if (!response) throw new Error("Response is not Valid")
6 | const data = await response.json()
7 | return data
8 | }
9 |
10 | export default fetchFollowers
11 |
--------------------------------------------------------------------------------
/Array.from-Pagination/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Pagination Starter
7 |
8 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
Loading...
20 |
21 |
22 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Array.from-Pagination/paginate.js:
--------------------------------------------------------------------------------
1 | const paginate = (followers) => {
2 | let itemsPerPage= 10;
3 | let numberOfPages= followers.length/itemsPerPage
4 | let finalPages= Array.from({length:numberOfPages},(_,index)=>{
5 | const start= index*itemsPerPage
6 | return followers.slice(start,start+itemsPerPage)
7 | })
8 | return finalPages
9 | };
10 |
11 | export default paginate
12 |
--------------------------------------------------------------------------------
/Array.from-Pagination/styles.css:
--------------------------------------------------------------------------------
1 | /*
2 | ===============
3 | Variables
4 | ===============
5 | */
6 |
7 | :root {
8 | /* dark shades of primary color*/
9 | --clr-primary-1: hsl(205, 86%, 17%);
10 | --clr-primary-2: hsl(205, 77%, 27%);
11 | --clr-primary-3: hsl(205, 72%, 37%);
12 | --clr-primary-4: hsl(205, 63%, 48%);
13 | /* primary/main color */
14 | --clr-primary-5: hsl(205, 78%, 60%);
15 | /* lighter shades of primary color */
16 | --clr-primary-6: hsl(205, 89%, 70%);
17 | --clr-primary-7: hsl(205, 90%, 76%);
18 | --clr-primary-8: hsl(205, 86%, 81%);
19 | --clr-primary-9: hsl(205, 90%, 88%);
20 | --clr-primary-10: hsl(205, 100%, 96%);
21 | /* darkest grey - used for headings */
22 | --clr-grey-1: hsl(209, 61%, 16%);
23 | --clr-grey-2: hsl(211, 39%, 23%);
24 | --clr-grey-3: hsl(209, 34%, 30%);
25 | --clr-grey-4: hsl(209, 28%, 39%);
26 | /* grey used for paragraphs */
27 | --clr-grey-5: hsl(210, 22%, 49%);
28 | --clr-grey-6: hsl(209, 23%, 60%);
29 | --clr-grey-7: hsl(211, 27%, 70%);
30 | --clr-grey-8: hsl(210, 31%, 80%);
31 | --clr-grey-9: hsl(212, 33%, 89%);
32 | --clr-grey-10: hsl(210, 36%, 96%);
33 | --clr-white: #fff;
34 | --clr-red-dark: hsl(360, 67%, 44%);
35 | --clr-red-light: hsl(360, 71%, 66%);
36 | --clr-green-dark: hsl(125, 67%, 44%);
37 | --clr-green-light: hsl(125, 71%, 66%);
38 | --clr-black: #222;
39 |
40 | --transition: all 0.3s linear;
41 | --spacing: 0.1rem;
42 | --radius: 0.75rem;
43 | --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
44 | --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
45 | --max-width: 1170px;
46 | --fixed-width: 620px;
47 | }
48 | /*
49 | ===============
50 | Global Styles
51 | ===============
52 | */
53 |
54 | *,
55 | ::after,
56 | ::before {
57 | margin: 0;
58 | padding: 0;
59 | box-sizing: border-box;
60 | }
61 | body {
62 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
63 | Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
64 | background: var(--clr-grey-10);
65 | color: var(--clr-grey-1);
66 | line-height: 1.5;
67 | font-size: 0.875rem;
68 | }
69 | ul {
70 | list-style-type: none;
71 | }
72 | a {
73 | text-decoration: none;
74 | }
75 | h1,
76 | h2,
77 | h3,
78 | h4 {
79 | letter-spacing: var(--spacing);
80 | text-transform: capitalize;
81 | line-height: 1.25;
82 | margin-bottom: 0.75rem;
83 | }
84 | h1 {
85 | font-size: 2.75rem;
86 | }
87 | h2 {
88 | font-size: 2rem;
89 | }
90 | h3 {
91 | font-size: 1.25rem;
92 | }
93 | h4 {
94 | font-size: 0.875rem;
95 | }
96 | p {
97 | margin-bottom: 1.25rem;
98 | color: var(--clr-grey-5);
99 | }
100 | @media screen and (min-width: 800px) {
101 | h1 {
102 | font-size: 3rem;
103 | }
104 | h2 {
105 | font-size: 2.5rem;
106 | }
107 | h3 {
108 | font-size: 1.75rem;
109 | }
110 | h4 {
111 | font-size: 1rem;
112 | }
113 | body {
114 | font-size: 1rem;
115 | }
116 | h1,
117 | h2,
118 | h3,
119 | h4 {
120 | line-height: 1;
121 | }
122 | }
123 |
124 | /*
125 | ===============
126 | Pagination
127 | ===============
128 | */
129 | .section-title {
130 | text-align: center;
131 | margin: 4rem 0 6rem 0;
132 | }
133 | .underline {
134 | width: 8rem;
135 | height: 0.25rem;
136 | background: var(--clr-primary-5);
137 | margin: 0 auto;
138 | }
139 | .followers {
140 | width: 90vw;
141 | max-width: var(--max-width);
142 | margin: 5rem auto;
143 | }
144 | .container {
145 | display: grid;
146 | gap: 2rem;
147 | margin-bottom: 4rem;
148 | }
149 | @media screen and (min-width: 576px) {
150 | .container {
151 | grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
152 | }
153 | }
154 | .card {
155 | background: var(--clr-white);
156 | border-radius: var(--radius);
157 | box-shadow: var(--light-shadow);
158 | padding: 2rem 1.5rem;
159 | text-align: center;
160 | }
161 | .card img {
162 | width: 130px;
163 | height: 130px;
164 | border-radius: 50%;
165 | object-fit: cover;
166 | margin-bottom: 0.75rem;
167 | }
168 |
169 | .card h4 {
170 | margin-bottom: 1.5rem;
171 | font-size: 0.85rem;
172 | color: var(--clr-grey-5);
173 | }
174 |
175 | .btn {
176 | padding: 0.35rem 0.75rem;
177 | letter-spacing: 1.6px;
178 | font-size: 0.75rem;
179 | color: var(--clr-white);
180 | background: var(--clr-primary-5);
181 | border-radius: var(--radius);
182 | border-color: transparent;
183 | text-transform: uppercase;
184 | transition: var(--transition);
185 | cursor: pointer;
186 | }
187 |
188 | .btn-container {
189 | display: flex;
190 | justify-content: center;
191 | flex-wrap: wrap;
192 | }
193 | .page-btn {
194 | width: 2rem;
195 | height: 2rem;
196 | background: var(--clr-primary-7);
197 | border-color: transparent;
198 | border-radius: 5px;
199 | cursor: pointer;
200 | margin: 0.5rem;
201 | transition: var(--transition);
202 | }
203 | .active-btn {
204 | background: var(--clr-primary-1);
205 | color: var(--clr-white);
206 | }
207 | .prev-btn,
208 | .next-btn {
209 | background: transparent;
210 | border-color: transparent;
211 | font-weight: bold;
212 | text-transform: capitalize;
213 | letter-spacing: var(--spacing);
214 | margin: 0.5rem;
215 | font-size: 1rem;
216 | cursor: pointer;
217 | }
218 |
219 | @media screen and (min-width: 775px) {
220 | .btn-container {
221 | margin: 0 auto;
222 | max-width: 900px;
223 | }
224 | }
225 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/FetchExample/app.js:
--------------------------------------------------------------------------------
1 |
2 | const button = document.querySelector(".fetch-data");
3 | const stories = document.querySelector(".stories");
4 | const nodata = document.querySelector(".loading");
5 | const noData= document.querySelector('.no-data')
6 | // import axios from 'axios'
7 |
8 | let buttonClickCount = 0;
9 | async function fetchdata() {
10 | let buttonClicked = true;
11 | let fetchdata = true;
12 | buttonClickCount++;
13 | try {
14 | buttonClicked && nodata.classList.add("d-block");
15 | noData.style.display='none'
16 | buttonClicked = false;
17 | if (buttonClickCount > 1 && fetchdata) {
18 | stories.style.display = "none";
19 | noData.style.display='none'
20 | }
21 | const response = await fetch("https://hn.algolia.com/api/v1/search?");
22 | const data = await response.json();
23 | fetchdata && nodata.classList.replace("d-block", "d-none");
24 | fetchdata && (stories.style.display = "grid");
25 | noData.style.display='none'
26 | fetchdata = false;
27 | stories.innerHTML = data?.hits
28 | ?.map((story) => {
29 | const { objectID, title, num_comments , points, author } = story;
30 | return `
31 | ${title}
32 |
33 | ${points} points by ${author} | ${num_comments}
34 | comments
35 |
36 | `;
37 | })
38 | .join("");
39 |
40 | console.log(fetchdata);
41 | } catch (error) {
42 | throw new Error(error)
43 | }
44 |
45 | }
46 | button.addEventListener("click", fetchdata);
47 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/FetchExample/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Document
7 |
8 |
9 |
10 |
11 |
12 |
13 | There is no data, Click on the below button to view data
14 |
15 |
16 | fetch data
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/FetchExample/style.css:
--------------------------------------------------------------------------------
1 | /*
2 | ===============
3 | Variables
4 | ===============
5 | */
6 |
7 | :root {
8 | /* dark shades of primary color*/
9 | --clr-primary-1: hsl(205, 86%, 17%);
10 | --clr-primary-2: hsl(205, 77%, 27%);
11 | --clr-primary-3: hsl(205, 72%, 37%);
12 | --clr-primary-4: hsl(205, 63%, 48%);
13 | /* primary/main color */
14 | --clr-primary-5: hsl(205, 78%, 60%);
15 | /* lighter shades of primary color */
16 | --clr-primary-6: hsl(205, 89%, 70%);
17 | --clr-primary-7: hsl(205, 90%, 76%);
18 | --clr-primary-8: hsl(205, 86%, 81%);
19 | --clr-primary-9: hsl(205, 90%, 88%);
20 | --clr-primary-10: hsl(205, 100%, 96%);
21 | /* darkest grey - used for headings */
22 | --clr-grey-1: hsl(209, 61%, 16%);
23 | --clr-grey-2: hsl(211, 39%, 23%);
24 | --clr-grey-3: hsl(209, 34%, 30%);
25 | --clr-grey-4: hsl(209, 28%, 39%);
26 | /* grey used for paragraphs */
27 | --clr-grey-5: hsl(210, 22%, 49%);
28 | --clr-grey-6: hsl(209, 23%, 60%);
29 | --clr-grey-7: hsl(211, 27%, 70%);
30 | --clr-grey-8: hsl(210, 31%, 80%);
31 | --clr-grey-9: hsl(212, 33%, 89%);
32 | --clr-grey-10: hsl(210, 36%, 96%);
33 | --clr-white: #fff;
34 | --clr-red-dark: hsl(360, 67%, 44%);
35 | --clr-red-light: hsl(360, 71%, 66%);
36 | --clr-green-dark: hsl(125, 67%, 44%);
37 | --clr-green-light: hsl(125, 71%, 66%);
38 | --clr-black: #222;
39 | --transition: all 0.3s linear;
40 | --spacing: 0.1rem;
41 | --radius: 0.25rem;
42 | --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
43 | --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
44 | --max-width: 1170px;
45 | --fixed-width: 620px;
46 | }
47 | /*
48 | ===============
49 | Global Styles
50 | ===============
51 | */
52 | .d-none {
53 | display: none;
54 | }
55 | .d-block {
56 | display: block;
57 | }
58 | *,
59 | ::after,
60 | ::before {
61 | margin: 0;
62 | padding: 0;
63 | box-sizing: border-box;
64 | }
65 | body {
66 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
67 | Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
68 | background: var(--clr-grey-10);
69 | color: var(--clr-grey-1);
70 | line-height: 1.5;
71 | font-size: 0.875rem;
72 | }
73 | ul {
74 | list-style-type: none;
75 | }
76 | a {
77 | text-decoration: none;
78 | }
79 | h1,
80 | h2,
81 | h3,
82 | h4 {
83 | letter-spacing: var(--spacing);
84 | text-transform: capitalize;
85 | line-height: 1.25;
86 | margin-bottom: 0.75rem;
87 | }
88 | h1 {
89 | font-size: 2.5rem;
90 | }
91 | h2 {
92 | font-size: 2rem;
93 | }
94 | h3 {
95 | font-size: 1.25rem;
96 | }
97 | h4 {
98 | font-size: 0.875rem;
99 | }
100 | p {
101 | margin-bottom: 1.25rem;
102 | color: var(--clr-grey-3);
103 | }
104 | @media screen and (min-width: 800px) {
105 | h1 {
106 | font-size: 3rem;
107 | }
108 | h2 {
109 | font-size: 2.5rem;
110 | }
111 | h3 {
112 | font-size: 1.75rem;
113 | }
114 | h4 {
115 | font-size: 1rem;
116 | }
117 | body {
118 | font-size: 1rem;
119 | }
120 | h1,
121 | h2,
122 | h3,
123 | h4 {
124 | line-height: 1;
125 | }
126 | }
127 | /* global classes */
128 |
129 | /* section */
130 | .section {
131 | width: 90vw;
132 | margin: 0 auto;
133 | max-width: var(--max-width);
134 | }
135 |
136 | @media screen and (min-width: 992px) {
137 | .section {
138 | width: 95vw;
139 | }
140 | }
141 |
142 | /*
143 | ===============
144 | Search
145 | ===============
146 | */
147 | .search-form {
148 | width: 90vw;
149 | max-width: var(--max-width);
150 | margin: 0 auto;
151 | margin-top: 5rem;
152 | margin-bottom: 3rem;
153 | }
154 | .form-input {
155 | width: 100%;
156 | border: none;
157 | border-bottom: 3px solid var(--clr-grey-8);
158 | max-width: 600px;
159 | background: transparent;
160 | padding: 1rem;
161 | font-size: 1rem;
162 | color: var(--clr-grey-3);
163 | text-transform: uppercase;
164 | letter-spacing: var(--spacing);
165 | margin-top: 1rem;
166 | }
167 |
168 | /*
169 | ===============
170 | Buttons
171 | ===============
172 | */
173 |
174 | .btn-container {
175 | width: 90vw;
176 | max-width: var(--max-width);
177 | margin: 0 auto;
178 | margin-bottom: 1.5rem;
179 | display: flex;
180 | justify-content: center;
181 | align-items: center;
182 | }
183 | .btn-container p {
184 | margin-bottom: 0;
185 | font-size: 1.2rem;
186 | font-weight: bold;
187 | }
188 |
189 | .btn-container button {
190 | margin: 1rem;
191 | padding: 0.25rem 0.5rem;
192 | text-transform: capitalize;
193 | font-weight: bold;
194 | border-color: transparent;
195 | background: var(--clr-primary-5);
196 | border-radius: var(--radius);
197 | color: var(--clr-white);
198 | letter-spacing: var(--spacing);
199 | cursor: pointer;
200 | }
201 | .btn-container button:disabled {
202 | cursor: not-allowed;
203 | }
204 |
205 | /*
206 | ===============
207 | Spinner
208 | ===============
209 | */
210 | .spinner-container {
211 | position: relative;
212 | }
213 | @keyframes spinner {
214 | to {
215 | transform: rotate(360deg);
216 | }
217 | }
218 |
219 | .loading {
220 | width: 6rem;
221 | height: 6rem;
222 | margin: 0 auto;
223 | margin-top: 10rem;
224 | border-radius: 50%;
225 | border: 3px solid #ccc;
226 | border-top-color: var(--clr-primary-5);
227 | animation: spinner 0.6s linear infinite;
228 | }
229 |
230 | /*
231 | ===============
232 | Story
233 | ===============
234 | */
235 | .stories {
236 | width: 90vw;
237 | max-width: var(--max-width);
238 | margin: 0 auto;
239 | margin-bottom: 5rem;
240 | display: grid;
241 | gap: 2rem;
242 | }
243 | @media screen and (min-width: 992px) {
244 | .stories {
245 | display: grid;
246 | grid-template-columns: 1fr 1fr;
247 | /* align-items: start; */
248 | }
249 | }
250 | .story {
251 | background: var(--clr-white);
252 | border-radius: var(--radius);
253 | padding: 1rem 2rem;
254 | }
255 |
256 | .title {
257 | line-height: 1.5;
258 | margin-bottom: 0.25rem;
259 | }
260 | .info {
261 | margin-bottom: 0.5rem;
262 | color: var(--clr-grey-5);
263 | }
264 |
265 | .read-link {
266 | font-size: 0.85rem;
267 | margin-right: 0.75rem;
268 | text-transform: capitalize;
269 | color: var(--clr-primary-5);
270 | }
271 |
272 | .remove-btn {
273 | background: transparent;
274 | color: var(--clr-red-dark);
275 | border-color: transparent;
276 | cursor: pointer;
277 | text-transform: capitalize;
278 | font-size: 0.85rem;
279 | }
280 |
281 | .no-data {
282 | text-align: center;
283 | font-weight: 500;
284 | font-size: 20px;
285 | text-decoration: underline;
286 | font-style: italic;
287 | margin-top: 1.25rem;
288 | }
289 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/Promise-example/app.js:
--------------------------------------------------------------------------------
1 | const button = document.querySelector('.btn');
2 | const imgContainer = document.querySelector('.image-container');
3 | const loadingText = document.querySelector('.loading-text')
4 |
5 | const url = 'https://source.unsplash.com/random';
6 |
7 | button.addEventListener('click', () => {
8 | Loadimage(url)
9 | .then(resp => {
10 | if (!resp) {
11 | throw new Error('No Image available');
12 | }
13 | imgContainer.appendChild(resp);
14 | })
15 | .catch(error => {
16 | console.log(error);
17 | });
18 | });
19 |
20 | const Loadimage = (url) => {
21 | return new Promise((resolve, reject) => {
22 | let img = new Image();
23 | loadingText.style.display = 'block';
24 | img.addEventListener('load', () => {
25 | resolve(img);
26 | loadingText.style.display = 'none';
27 | });
28 | img.addEventListener('error', () => {
29 | reject(new Error(`Opps there is error in your ${url}`));
30 | });
31 | img.src = url;
32 | });
33 | };
34 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/Promise-example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Asynchronous Javascript
9 |
43 |
44 |
45 |
46 |
Click me to Trigger
47 |
Loading....
48 |
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/Promise/app.js:
--------------------------------------------------------------------------------
1 | const BASEURL = "https://dummyjson.com";
2 | //Promise are used to run Asynchronous operations in javascript,
3 | //Prior to promises events and callback functions were used but they had limited functionalities and created unmanageable code.
4 | //Promise are use to avoid the callback hell
5 | //pending, Rejected, Fulfilled
6 |
7 | // let Promises = new Promise((resolve, reject) => {
8 | // const firstvalue = 'uncle tom'
9 | // const secondvalue = 'uncle tom'
10 | // if (firstvalue === secondvalue) {
11 | // resolve()
12 | // } else {
13 | // reject()
14 | // }
15 |
16 | // })
17 |
18 | // Promises.then(() => {
19 | // console.log('Success');
20 | // }).catch(() => {
21 | // console.log('Some error has occurred');
22 | // });
23 |
24 | // Examples
25 | let success = true;
26 |
27 | // function getUsers() {
28 | // return new Promise((resolve, reject) => {
29 | // setTimeout(() => {
30 | // if (success) {
31 | // resolve([
32 | // { username: 'john', email: 'john@test.com' },
33 | // { username: 'jane', email: 'jane@test.com' },
34 | // ]);
35 | // } else {
36 | // reject('Failed to the user list');
37 | // }
38 | // }, 1000);
39 | // });
40 | // }
41 |
42 | // function onFulfilled(users) {
43 | // console.log(users);
44 | // }
45 | // function onRejected(error) {
46 | // console.log(error);
47 | // }
48 |
49 | // const promise = getUsers();
50 | // promise.then(onFulfilled, onRejected);
51 |
52 |
53 | //Created generic the "customFetch" function to take in a endpoint and handle errors (including a wrong endpoint)
54 | // const customFetch = async (endpoints, options) => {
55 | // try {
56 | // const res = await fetch(`${BASEURL}/${endpoints}`, { ...options });
57 | // if (!res.ok) throw new Error("response not valid")
58 | // const json = await res.json();
59 | // return json;
60 | // } catch (error) {
61 | // console.log(error.message);
62 | // }
63 | // }
64 | async function customFetch(endpoint, options) {
65 | try {
66 | const res = await fetch(`${BASEURL}/${endpoint}`, { ...options });
67 | if (!res.ok) {
68 | throw new Error(`${res.status}: ${res.statusText}`)
69 | }
70 | const json = await res.json();
71 | return json
72 | } catch (err) {
73 | console.error(err.message)
74 | }
75 | }
76 | //Create a function called "getProducts" that returns all the products in an array. Then list all the products in the DOM in an unordered list.
77 |
78 | const getCommentsOnUsersPosts = async (id) => {
79 | if (!id) throw Error('Please enter value')
80 | const postByUser = await customFetch(`users/${id}/posts`);
81 | const singlePost = await postByUser?.posts?.map((post) => {
82 |
83 | const { title, body, reactions } = post
84 | return { title, body, reactions }
85 | })
86 | return singlePost
87 | }
88 |
89 |
90 | async function abc() {
91 | const data = await getCommentsOnUsersPosts(5)
92 | console.log(data);
93 | }
94 |
95 | abc()
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/Promise/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Javascript Promise
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/PromiseAsyncNuggets/app.js:
--------------------------------------------------------------------------------
1 | const promise = new Promise((resolve, reject) => {
2 | setTimeout(() => {
3 | resolve("promise resolved");
4 | }, 3000);
5 | });
6 |
7 | // async,await
8 | async function PromiseHandler() {
9 | await promise;
10 | console.log("value");
11 | }
12 |
13 | PromiseHandler();
14 |
15 | function getpromise() {
16 | promise.then((res) => console.log(res));
17 | console.log("value");
18 | }
19 | getpromise();
20 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/PromiseAsyncNuggets/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
Document
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/PromiseAsyncNuggets/temp.js:
--------------------------------------------------------------------------------
1 | function counter() {
2 | let count = 0;
3 | function abc() {
4 | return ++count;
5 | };
6 | return abc
7 | }
8 | const increment = counter();
9 | console.log(increment());
10 | console.log(increment());
11 | console.log(increment());
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/ajax-xhr/Normalize.css:
--------------------------------------------------------------------------------
1 | /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
2 |
3 | /* Document
4 | ========================================================================== */
5 |
6 | /**
7 | * 1. Correct the line height in all browsers.
8 | * 2. Prevent adjustments of font size after orientation changes in iOS.
9 | */
10 |
11 | html {
12 | line-height: 1.15; /* 1 */
13 | -webkit-text-size-adjust: 100%; /* 2 */
14 | }
15 |
16 | /* Sections
17 | ========================================================================== */
18 |
19 | /**
20 | * Remove the margin in all browsers.
21 | */
22 |
23 | body {
24 | margin: 0;
25 | }
26 |
27 | /**
28 | * Render the `main` element consistently in IE.
29 | */
30 |
31 | main {
32 | display: block;
33 | }
34 |
35 | /**
36 | * Correct the font size and margin on `h1` elements within `section` and
37 | * `article` contexts in Chrome, Firefox, and Safari.
38 | */
39 |
40 | h1 {
41 | font-size: 2em;
42 | margin: 0.67em 0;
43 | }
44 |
45 | /* Grouping content
46 | ========================================================================== */
47 |
48 | /**
49 | * 1. Add the correct box sizing in Firefox.
50 | * 2. Show the overflow in Edge and IE.
51 | */
52 |
53 | hr {
54 | box-sizing: content-box; /* 1 */
55 | height: 0; /* 1 */
56 | overflow: visible; /* 2 */
57 | }
58 |
59 | /**
60 | * 1. Correct the inheritance and scaling of font size in all browsers.
61 | * 2. Correct the odd `em` font sizing in all browsers.
62 | */
63 |
64 | pre {
65 | font-family: monospace, monospace; /* 1 */
66 | font-size: 1em; /* 2 */
67 | }
68 |
69 | /* Text-level semantics
70 | ========================================================================== */
71 |
72 | /**
73 | * Remove the gray background on active links in IE 10.
74 | */
75 |
76 | a {
77 | background-color: transparent;
78 | }
79 |
80 | /**
81 | * 1. Remove the bottom border in Chrome 57-
82 | * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
83 | */
84 |
85 | abbr[title] {
86 | border-bottom: none; /* 1 */
87 | text-decoration: underline; /* 2 */
88 | text-decoration: underline dotted; /* 2 */
89 | }
90 |
91 | /**
92 | * Add the correct font weight in Chrome, Edge, and Safari.
93 | */
94 |
95 | b,
96 | strong {
97 | font-weight: 500;
98 | }
99 |
100 | /**
101 | * 1. Correct the inheritance and scaling of font size in all browsers.
102 | * 2. Correct the odd `em` font sizing in all browsers.
103 | */
104 |
105 | code,
106 | kbd,
107 | samp {
108 | font-family: monospace, monospace; /* 1 */
109 | font-size: 1em; /* 2 */
110 | }
111 |
112 | /**
113 | * Add the correct font size in all browsers.
114 | */
115 |
116 | small {
117 | font-size: 80%;
118 | }
119 |
120 | /**
121 | * Prevent `sub` and `sup` elements from affecting the line height in
122 | * all browsers.
123 | */
124 |
125 | sub,
126 | sup {
127 | font-size: 75%;
128 | line-height: 0;
129 | position: relative;
130 | vertical-align: baseline;
131 | }
132 |
133 | sub {
134 | bottom: -0.25em;
135 | }
136 |
137 | sup {
138 | top: -0.5em;
139 | }
140 |
141 | /* Embedded content
142 | ========================================================================== */
143 |
144 | /**
145 | * Remove the border on images inside links in IE 10.
146 | */
147 |
148 | img {
149 | border-style: none;
150 | }
151 |
152 | /* Forms
153 | ========================================================================== */
154 |
155 | /**
156 | * 1. Change the font styles in all browsers.
157 | * 2. Remove the margin in Firefox and Safari.
158 | */
159 |
160 | button,
161 | input,
162 | optgroup,
163 | select,
164 | textarea {
165 | font-family: inherit; /* 1 */
166 | font-size: 100%; /* 1 */
167 | line-height: 1.15; /* 1 */
168 | margin: 0; /* 2 */
169 | }
170 |
171 | /**
172 | * Show the overflow in IE.
173 | * 1. Show the overflow in Edge.
174 | */
175 |
176 | button,
177 | input {
178 | /* 1 */
179 | overflow: visible;
180 | }
181 |
182 | /**
183 | * Remove the inheritance of text transform in Edge, Firefox, and IE.
184 | * 1. Remove the inheritance of text transform in Firefox.
185 | */
186 |
187 | button,
188 | select {
189 | /* 1 */
190 | text-transform: none;
191 | }
192 |
193 | /**
194 | * Correct the inability to style clickable types in iOS and Safari.
195 | */
196 |
197 | button,
198 | [type='button'],
199 | [type='reset'],
200 | [type='submit'] {
201 | -webkit-appearance: button;
202 | }
203 |
204 | /**
205 | * Remove the inner border and padding in Firefox.
206 | */
207 |
208 | button::-moz-focus-inner,
209 | [type='button']::-moz-focus-inner,
210 | [type='reset']::-moz-focus-inner,
211 | [type='submit']::-moz-focus-inner {
212 | border-style: none;
213 | padding: 0;
214 | }
215 |
216 | /**
217 | * Restore the focus styles unset by the previous rule.
218 | */
219 |
220 | button:-moz-focusring,
221 | [type='button']:-moz-focusring,
222 | [type='reset']:-moz-focusring,
223 | [type='submit']:-moz-focusring {
224 | outline: 1px dotted ButtonText;
225 | }
226 |
227 | /**
228 | * Correct the padding in Firefox.
229 | */
230 |
231 | fieldset {
232 | padding: 0.35em 0.75em 0.625em;
233 | }
234 |
235 | /**
236 | * 1. Correct the text wrapping in Edge and IE.
237 | * 2. Correct the color inheritance from `fieldset` elements in IE.
238 | * 3. Remove the padding so developers are not caught out when they zero out
239 | * `fieldset` elements in all browsers.
240 | */
241 |
242 | legend {
243 | box-sizing: border-box; /* 1 */
244 | color: inherit; /* 2 */
245 | display: table; /* 1 */
246 | max-width: 100%; /* 1 */
247 | padding: 0; /* 3 */
248 | white-space: normal; /* 1 */
249 | }
250 |
251 | /**
252 | * Add the correct vertical alignment in Chrome, Firefox, and Opera.
253 | */
254 |
255 | progress {
256 | vertical-align: baseline;
257 | }
258 |
259 | /**
260 | * Remove the default vertical scrollbar in IE 10+.
261 | */
262 |
263 | textarea {
264 | overflow: auto;
265 | }
266 |
267 | /**
268 | * 1. Add the correct box sizing in IE 10.
269 | * 2. Remove the padding in IE 10.
270 | */
271 |
272 | [type='checkbox'],
273 | [type='radio'] {
274 | box-sizing: border-box; /* 1 */
275 | padding: 0; /* 2 */
276 | }
277 |
278 | /**
279 | * Correct the cursor style of increment and decrement buttons in Chrome.
280 | */
281 |
282 | [type='number']::-webkit-inner-spin-button,
283 | [type='number']::-webkit-outer-spin-button {
284 | height: auto;
285 | }
286 |
287 | /**
288 | * 1. Correct the odd appearance in Chrome and Safari.
289 | * 2. Correct the outline style in Safari.
290 | */
291 |
292 | [type='search'] {
293 | -webkit-appearance: textfield; /* 1 */
294 | outline-offset: -2px; /* 2 */
295 | }
296 |
297 | /**
298 | * Remove the inner padding in Chrome and Safari on macOS.
299 | */
300 |
301 | [type='search']::-webkit-search-decoration {
302 | -webkit-appearance: none;
303 | }
304 |
305 | /**
306 | * 1. Correct the inability to style clickable types in iOS and Safari.
307 | * 2. Change font properties to `inherit` in Safari.
308 | */
309 |
310 | ::-webkit-file-upload-button {
311 | -webkit-appearance: button; /* 1 */
312 | font: inherit; /* 2 */
313 | }
314 |
315 | /* Interactive
316 | ========================================================================== */
317 |
318 | /*
319 | * Add the correct display in Edge, IE 10+, and Firefox.
320 | */
321 |
322 | details {
323 | display: block;
324 | }
325 |
326 | /*
327 | * Add the correct display in all browsers.
328 | */
329 |
330 | summary {
331 | display: list-item;
332 | }
333 |
334 | /* Misc
335 | ========================================================================== */
336 |
337 | /**
338 | * Add the correct display in IE 10+.
339 | */
340 |
341 | template {
342 | display: none;
343 | }
344 |
345 | /**
346 | * Add the correct display in IE 10.
347 | */
348 |
349 | [hidden] {
350 | display: none;
351 | }
352 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/ajax-xhr/app.js:
--------------------------------------------------------------------------------
1 | const btn = document.querySelector('.btn');
2 | const loading = document.querySelector('.loading');
3 | const xhr = new XMLHttpRequest();
4 | // Sepcify method and endpoint/URL
5 | // xhr.open('GET', './movies.json');
6 | xhr.open('GET', 'https://api.github.com/users/bradtraversy/repos')
7 | // readyState has 5 possible values
8 | // - 0: request not initialized
9 | // - 1: server connection established
10 | // - 2: request received
11 | // - 3: processing request
12 | // - 4: request finished and response is ready
13 | xhr.onreadystatechange = function () {
14 | if (this.readyState == 2 && this.status !== 200) {
15 | console.log('000');
16 | loading.style.display='block'
17 | return
18 | }
19 | if (this.readyState === 4 && this.status === 200) {
20 | const Users = JSON.parse(this.responseText)
21 | // loading.style.display='none'
22 | const result = Users?.map((User) => {
23 | const { name, description } = User
24 | return `
25 |
26 | ${name}
28 | `
29 | })
30 | document.querySelector('#results').innerHTML = result;
31 | }
32 | }
33 | const sendRequest = () => {
34 | xhr.send();
35 | }
36 |
37 | btn.addEventListener('click', sendRequest)
38 |
39 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/ajax-xhr/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | AJAX & XHR Object
10 |
11 |
12 |
13 |
AJAX & XHR Object
14 |
15 |
Click me to send Request
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/Asynchronous-JavaScript/ajax-xhr/main.css:
--------------------------------------------------------------------------------
1 | *,
2 | ::after,
3 | ::before {
4 | margin: 0;
5 | padding: 0;
6 | box-sizing: border-box;
7 | }
8 |
9 | html {
10 | font-size: 100%;
11 | } /*16px*/
12 |
13 | :root {
14 | /* colors */
15 | --primary-100: #e2e0ff;
16 | --primary-200: #c1beff;
17 | --primary-300: #a29dff;
18 | --primary-400: #837dff;
19 | --primary-500: #645cff;
20 | --primary-600: #504acc;
21 | --primary-700: #3c3799;
22 | --primary-800: #282566;
23 | --primary-900: #141233;
24 |
25 | /* grey */
26 | --grey-50: #f8fafc;
27 | --grey-100: #f1f5f9;
28 | --grey-200: #e2e8f0;
29 | --grey-300: #cbd5e1;
30 | --grey-400: #94a3b8;
31 | --grey-500: #64748b;
32 | --grey-600: #475569;
33 | --grey-700: #334155;
34 | --grey-800: #1e293b;
35 | --grey-900: #0f172a;
36 | /* rest of the colors */
37 | --black: #222;
38 | --white: #fff;
39 | --red-light: #f8d7da;
40 | --red-dark: #842029;
41 | --green-light: #d1e7dd;
42 | --green-dark: #0f5132;
43 |
44 | --small-text: 0.875rem;
45 | --extra-small-text: 0.7em;
46 | /* rest of the vars */
47 | --backgroundColor: var(--grey-50);
48 | --textColor: var(--grey-900);
49 | --borderRadius: 0.25rem;
50 | --letterSpacing: 1px;
51 | --transition: 0.3s ease-in-out all;
52 | --max-width: 1120px;
53 | --fixed-width: 600px;
54 |
55 | /* box shadow*/
56 | --shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
57 | --shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
58 | 0 2px 4px -1px rgba(0, 0, 0, 0.06);
59 | --shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
60 | 0 4px 6px -2px rgba(0, 0, 0, 0.05);
61 | --shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
62 | 0 10px 10px -5px rgba(0, 0, 0, 0.04);
63 | }
64 |
65 | body {
66 | background: var(--backgroundColor);
67 | font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
68 | Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
69 | font-weight: 400;
70 | line-height: 1;
71 | color: var(--textColor);
72 | }
73 | p {
74 | margin: 0;
75 | }
76 | h1,
77 | h2,
78 | h3,
79 | h4,
80 | h5 {
81 | margin: 0;
82 | font-family: var(--headingFont);
83 | font-weight: 400;
84 | line-height: 1;
85 | text-transform: capitalize;
86 | letter-spacing: var(--letterSpacing);
87 | }
88 |
89 | h1 {
90 | font-size: 3.052rem;
91 | }
92 |
93 | h2 {
94 | font-size: 2.441rem;
95 | }
96 |
97 | h3 {
98 | font-size: 1.953rem;
99 | }
100 |
101 | h4 {
102 | font-size: 1.563rem;
103 | }
104 |
105 | h5 {
106 | font-size: 1.25rem;
107 | }
108 |
109 | .text {
110 | margin-bottom: 1.5rem;
111 | max-width: 40em;
112 | }
113 |
114 | small,
115 | .text-small {
116 | font-size: var(--small-text);
117 | }
118 |
119 | a {
120 | text-decoration: none;
121 | }
122 | ul {
123 | list-style-type: none;
124 | padding: 0;
125 | margin-top: 30px;
126 | }
127 | li{
128 | margin-bottom: 15px;
129 | max-width: var(--fixed-width);
130 | background: var(--white);
131 | border-radius: var(--borderRadius);
132 | box-shadow: var(--shadow-2);
133 | padding: 0.8rem 0.8rem;
134 | }
135 | .results-main {
136 | padding: 15px;
137 | }
138 | .btn {
139 | cursor: pointer;
140 | color: var(--white);
141 | background: var(--primary-500);
142 | border: transparent;
143 | border-radius: var(--borderRadius);
144 | letter-spacing: var(--letterSpacing);
145 | padding: 0.375rem 0.75rem;
146 | box-shadow: var(--shadow-1);
147 | transition: var(--transition);
148 | text-transform: capitalize;
149 | display: inline-block;
150 | }
151 | .btn:hover {
152 | background: var(--primary-700);
153 | box-shadow: var(--shadow-3);
154 | }
155 | .btn-hipster {
156 | color: var(--primary-500);
157 | background: var(--primary-200);
158 | }
159 | .btn-hipster:hover {
160 | color: var(--primary-200);
161 | background: var(--primary-700);
162 | }
163 | .btn-block {
164 | width: 100%;
165 | }
166 |
167 | .mt-10{
168 | margin-top: 15px;
169 | }
170 |
171 | @keyframes spinner {
172 | to {
173 | transform: rotate(360deg);
174 | }
175 | }
176 |
177 | .loading {
178 | width: 6rem;
179 | height: 6rem;
180 | border: 5px solid var(--grey-400);
181 | border-radius: 50%;
182 | border-top-color: var(--primary-500);
183 | animation: spinner 0.6s linear infinite;
184 | margin: 0 auto;
185 | display: none;
186 | }
--------------------------------------------------------------------------------
/Call/Apply/Bind/app.js:
--------------------------------------------------------------------------------
1 | // Examples of call, Apply,Bind
2 |
3 | // Call Method
4 |
5 | function greet(name) {
6 | console.log(`Hello, ${name}! My name is ${this.name}.`);
7 | }
8 | const person = { name: "Amir Ali Anwar" }
9 | greet.call(person, 'amir')
10 |
11 | function sayHello(role) {
12 | console.log(`Hello, ${this.name}! and I am ${role}`);
13 | }
14 | const PetterObj = { name: "Peter" }
15 | const DocObj = { name: "Dr. Umair" }
16 | const intro = sayHello.call(PetterObj, 'Software Engineer')
17 | const DoctorInto= sayHello.call(DocObj,'Lawyer')
18 |
19 |
20 | // Apply Method
21 |
22 | function greets(name) {
23 | console.log(`Hello, ${name}! My name is ${this.name}.`);
24 | }
25 |
26 | const personInfo = { name: 'John' };
27 | const args = ['Alice', 30];
28 |
29 | greets.apply(personInfo, args);
30 |
31 | // Bind
32 |
33 | const BindPerson= greet.bind(personInfo)
34 | BindPerson('Brad')
35 |
36 |
37 |
38 | function minMovesToMedian(prices, k) {
39 | // Step 1: Sort the array
40 | prices.sort((a, b) => a - b);
41 |
42 | // Step 2: Calculate the current median index
43 | const n = prices.length;
44 | const medianIndex = Math.floor((n + 1) / 2) - 1; // Adjusted for zero-based index
45 |
46 | // Step 3: Check if the current median is already equal to k
47 | const currentMedian = prices[medianIndex];
48 | if (currentMedian === k) {
49 | return 0; // No moves needed
50 | }
51 |
52 | // Step 4 and 5: Adjust prices to make the median equal to k
53 | let moves = 0;
54 | for (let i = 0; i < n; i++) {
55 | const diff = k - prices[medianIndex];
56 | moves += Math.abs(diff);
57 | }
58 |
59 | return moves;
60 | }
61 |
62 | // Example usage:
63 | const prices = [1, 3, 5];
64 | const k = 4;
65 | const result = minMovesToMedian(prices, k);
66 | console.log(result); // Output the minimum number of moves
67 |
--------------------------------------------------------------------------------
/Css-variables/app.js:
--------------------------------------------------------------------------------
1 | const toggleBtn= document.querySelector('.btn')
2 | const root=document.querySelector(':root')
3 | const heading3= document.querySelector('.post h3')
4 | toggleBtn.addEventListener('click',()=>{
5 | const rootStyles = getComputedStyle(root);
6 | const headingStyles= getComputedStyle(heading3).getPropertyValue('letter-spacing')
7 | console.log(headingStyles);
8 | const current = rootStyles.getPropertyValue('--clr-bcg');
9 | const colorValue = current === '#fff' ? '#282c35' : '#fff';
10 | const fontValue = current === '#fff' ? '#fff' : '#282c35';
11 | root.style.setProperty('--clr-bcg',colorValue)
12 | root.style.setProperty('--clr-font',fontValue)
13 |
14 |
15 | })
16 | const variableHeading= document.querySelector('.post h3')
17 | const headingStyles= getComputedStyle(variableHeading).getPropertyValue('margin')
18 | console.log(headingStyles);
--------------------------------------------------------------------------------
/Css-variables/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Javascript Nuggets
7 |
8 |
9 |
10 |
11 |
12 |
13 |
MyBlog
14 | toggle
15 |
16 |
17 |
18 |
19 | CSS Variables
20 | Sunday 4th, 2020. 11 min read
21 |
22 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam
23 | dolores enim porro cupiditate molestias vitae sint libero quis illo
24 | dignissimos...
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/Css-variables/main.css:
--------------------------------------------------------------------------------
1 | :root {
2 | --clr-bcg: #fff;
3 | --clr-font: #282c35;
4 | --clr-primary: #d23669;
5 | }
6 |
7 | .dark-theme {
8 | --clr-bcg: #282c35;
9 | --clr-font: #fff;
10 | --clr-primary: #ffa7c4;
11 | }
12 | /* .light-theme {
13 | --clr-bcg: #fff;
14 | --clr-font: #282c35;
15 | --clr-primary: #d23669;
16 | } */
17 | *,
18 | ::after,
19 | ::before {
20 | margin: 0;
21 | padding: 0;
22 | box-sizing: border-box;
23 | }
24 | body {
25 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
26 | Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
27 | background: var(--clr-bcg);
28 | color: var(--clr-font);
29 | font-size: 0.875rem;
30 | transition: all 0.3s linear;
31 | }
32 |
33 | /*
34 | ===============
35 | Navbar
36 | ===============
37 | */
38 | .nav-center {
39 | width: 90vw;
40 | max-width: 600px;
41 | margin: 0 auto;
42 | display: flex;
43 | justify-content: space-between;
44 | align-items: center;
45 | padding: 2rem 0;
46 | }
47 | .nav-center h2 {
48 | font-size: 2.5rem;
49 | text-transform: capitalize;
50 | letter-spacing: 2px;
51 | margin-bottom: 0;
52 | }
53 |
54 | .articles {
55 | width: 90vw;
56 | max-width: 600px;
57 | margin: 0 auto;
58 | margin-top: 2rem;
59 | }
60 | .post h3 {
61 | color: var(--clr-primary);
62 | text-transform: capitalize;
63 | letter-spacing: 2px;
64 | margin-bottom: 0.25rem;
65 | }
66 | .date {
67 | margin-bottom: 0.75rem;
68 | font-style: italic;
69 | font-size: 0.75em;
70 | }
71 |
72 | .btn {
73 | background: var(--clr-primary);
74 | color: var(--clr-bcg);
75 | padding: 0.35rem 0.75rem;
76 | border-radius: 5px;
77 | border-color: transparent;
78 | text-transform: capitalize;
79 | transition: all 0.3s linear;
80 | font-weight: bold;
81 | letter-spacing: 2px;
82 | cursor: pointer;
83 | }
84 | .btn:hover {
85 | background: var(--clr-primary);
86 | color: var(--clr-bcg);
87 | }
88 |
--------------------------------------------------------------------------------
/Currying/app.js:
--------------------------------------------------------------------------------
1 | //Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).
2 |
3 | function curry(r) {
4 | return function (a) {
5 | return function (b) {
6 | return r(a, b)
7 | }
8 | }
9 | }
10 | function sum(a, b) {
11 | return a + b
12 | }
13 | let curryingsum = curry(sum)
14 | console.log(curryingsum(12)(20))
--------------------------------------------------------------------------------
/Currying/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Document
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Custom-video-Player/app.js:
--------------------------------------------------------------------------------
1 | console.log("hy there");
--------------------------------------------------------------------------------
/Custom-video-Player/asset/pexels.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Amir-ali-anwar/Javascript-nuggets/215e5e886ca7f6417552a3e2a4e6b55270e49fcd/Custom-video-Player/asset/pexels.mp4
--------------------------------------------------------------------------------
/Custom-video-Player/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Custom Video Player
8 |
9 |
10 |
11 |
12 |
13 |
Custom Video Player
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/Custom-video-Player/style.css:
--------------------------------------------------------------------------------
1 | * {
2 | padding: 0%;
3 | margin: 0%;
4 | -webkit-box-sizing: border-box;
5 | box-sizing: border-box;
6 | font-family: sans-serif;
7 | }
8 |
9 | body {
10 | background-color: aliceblue;
11 | }
12 |
13 | .container {
14 | width: 90%;
15 | display: -webkit-box;
16 | display: -ms-flexbox;
17 | display: flex;
18 | -webkit-box-align: center;
19 | -ms-flex-align: center;
20 | align-items: center;
21 | -webkit-box-pack: center;
22 | -ms-flex-pack: center;
23 | justify-content: center;
24 | -webkit-box-orient: vertical;
25 | -webkit-box-direction: normal;
26 | -ms-flex-direction: column;
27 | flex-direction: column;
28 | margin: 0 auto;
29 | position: relative;
30 | margin-top: 3rem;
31 | max-width: 610px;
32 | }
33 |
34 | .container video {
35 | width: 100%;
36 | margin-top: 2rem;
37 | }
38 |
39 | .container .controlCont {
40 | position: absolute;
41 | bottom: 0%;
42 | display: -webkit-box;
43 | display: -ms-flexbox;
44 | display: flex;
45 | -webkit-box-align: center;
46 | -ms-flex-align: center;
47 | align-items: center;
48 | -webkit-box-pack: center;
49 | -ms-flex-pack: center;
50 | justify-content: center;
51 | -webkit-box-orient: vertical;
52 | -webkit-box-direction: normal;
53 | -ms-flex-direction: column;
54 | flex-direction: column;
55 | width: 100%;
56 | padding: 0.5rem 0.7rem;
57 | opacity: 0;
58 | -webkit-transition: 0.3s ease opacity;
59 | transition: 0.3s ease opacity;
60 | }
61 |
62 | .container .controlCont .sliderCont {
63 | width: 100%;
64 | }
65 |
66 | .container .controlCont .sliderCont input {
67 | width: 100%;
68 | cursor: pointer;
69 | }
70 |
71 | .container .controlCont .sliderCont input {
72 | -webkit-appearance: none;
73 | -moz-appearance: none;
74 | appearance: none;
75 | background-color: rgba(255, 255, 255, 0.5);
76 | height: 4px;
77 | outline: none;
78 | background: -webkit-gradient(linear, left top, right top, from(#f00), color-stop(0%, #f00), color-stop(0%, rgba(255, 255, 255, 0.5)), to(rgba(255, 255, 255, 0.5)));
79 | background: linear-gradient(to right, #f00 0%, #f00 0%, rgba(255, 255, 255, 0.5) 0%, rgba(255, 255, 255, 0.5) 100%);
80 | }
81 |
82 | .container .controlCont .sliderCont input::-webkit-slider-thumb {
83 | -webkit-appearance: none;
84 | appearance: none;
85 | height: 4px;
86 | width: 4px;
87 | background-color: #f00;
88 | border: none;
89 | outline: none;
90 | border-radius: 0;
91 | -webkit-transition: 0.3s ease;
92 | transition: 0.3s ease;
93 | border-radius: 50%;
94 | -webkit-transition-property: height, width;
95 | transition-property: height, width;
96 | }
97 |
98 | .container .controlCont .sliderCont input::-moz-range-thumb {
99 | -moz-appearance: none;
100 | appearance: none;
101 | height: 4px;
102 | width: 4px;
103 | background-color: #f00;
104 | background-color: #f00;
105 | border: none;
106 | outline: none;
107 | border-radius: 0;
108 | -webkit-transition: 0.3s ease;
109 | transition: 0.3s ease;
110 | border-radius: 50%;
111 | -webkit-transition-property: height, width;
112 | transition-property: height, width;
113 | }
114 |
115 | .container .controlCont .sliderCont input::-moz-range-progress {
116 | -moz-appearance: none;
117 | appearance: none;
118 | background-color: #f00;
119 | height: 100%;
120 | }
121 |
122 | .container .controlCont .sliderCont input:hover::-webkit-slider-thumb {
123 | height: 12px;
124 | width: 12px;
125 | border-radius: 50%;
126 | }
127 |
128 | .container .controlCont .sliderCont input:hover::-moz-range-thumb {
129 | height: 12px;
130 | width: 12px;
131 | border-radius: 50%;
132 | }
133 |
134 | .container .controlCont .control {
135 | display: -webkit-box;
136 | display: -ms-flexbox;
137 | display: flex;
138 | -webkit-box-align: center;
139 | -ms-flex-align: center;
140 | align-items: center;
141 | -webkit-box-pack: justify;
142 | -ms-flex-pack: justify;
143 | justify-content: space-between;
144 | width: 100%;
145 | margin-top: 0.2rem;
146 | }
147 |
148 | .container .controlCont .control .controlLeft {
149 | display: -webkit-box;
150 | display: -ms-flexbox;
151 | display: flex;
152 | -webkit-box-align: center;
153 | -ms-flex-align: center;
154 | align-items: center;
155 | -webkit-box-pack: center;
156 | -ms-flex-pack: center;
157 | justify-content: center;
158 | }
159 |
160 | .container .controlCont .control .controlLeft .time {
161 | margin-left: 0.5rem;
162 | }
163 |
164 | .container .controlCont .control .controlLeft .time p {
165 | color: #f4f4f4;
166 | }
167 |
168 | .container .controlCont .control .controlRight {
169 | display: -webkit-box;
170 | display: -ms-flexbox;
171 | display: flex;
172 | -webkit-box-align: center;
173 | -ms-flex-align: center;
174 | align-items: center;
175 | -webkit-box-pack: center;
176 | -ms-flex-pack: center;
177 | justify-content: center;
178 | }
179 |
180 | .container .controlCont .control button {
181 | border: none;
182 | outline: none;
183 | background-color: transparent;
184 | font-size: 1.4rem;
185 | color: #f4f4f4;
186 | cursor: pointer;
187 | margin: 0 4px;
188 | display: -webkit-box;
189 | display: -ms-flexbox;
190 | display: flex;
191 | -webkit-box-align: center;
192 | -ms-flex-align: center;
193 | align-items: center;
194 | -webkit-box-pack: center;
195 | -ms-flex-pack: center;
196 | justify-content: center;
197 | padding: 0.4rem;
198 | border-radius: 50%;
199 | }
200 |
201 | .container .controlCont .control button:hover {
202 | background-color: rgba(255, 255, 255, 0.2);
203 | }
204 |
205 | .container .controlCont:hover {
206 | opacity: 1;
207 | }
208 | /*# sourceMappingURL=style.css.map */
--------------------------------------------------------------------------------
/Dynamic-Values/app.js:
--------------------------------------------------------------------------------
1 | const person ={
2 | name:'amir',
3 | state:'pakistan'
4 | }
5 | console.log(person)
6 |
7 | let newapp='job-portal'
8 | const app={
9 | [newapp]:true
10 | }
11 | console.log(app);
12 |
13 | const state={
14 | loading:true,
15 | name:"amir",
16 | job:'developer'
17 | }
18 | const updateState=(key='',value='')=>{
19 | state[key]= value
20 | }
21 | updateState('food',['banna','apple'])
22 | updateState('loading','false')
23 | updateState('name','Amir Ali Anwar')
24 |
25 | console.log(state);
--------------------------------------------------------------------------------
/Dynamic-Values/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Javascript Dynamic values
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Event-bubbling/Example-1/app.js:
--------------------------------------------------------------------------------
1 | const parent = document.querySelector('.child');
2 | parent.addEventListener('click', function(event) {
3 | console.log('Clicked on child element');
4 | event.stopPropagation()
5 | })
--------------------------------------------------------------------------------
/Event-bubbling/Example-1/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Event bubbling
8 |
22 |
23 |
24 |
25 | Parent
26 |
Child
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Event-bubbling/Example-2/app.js:
--------------------------------------------------------------------------------
1 | // Event Bubbling :
2 | // Event Bubbling is a concept in the DOM (Document Object Model).
3 | //it happens when an element receives an event, and that event bubbles up (or you can say is transmitted or propagated)
4 | //to its parent and ancestor elements in the DOM tree until it gets to the root element.
5 | // Event bubbing can be stopped by stopPropagation() that JavaScript provides
6 | const body = document.getElementsByTagName("body")[0]
7 | const div = document.getElementsByTagName("div")[0]
8 | const span = document.getElementsByTagName("span")[0]
9 | const button = document.getElementsByTagName("button")[0]
10 |
11 | body.addEventListener('click', () => {
12 | console.log("body was clicked")
13 | })
14 |
15 | div.addEventListener('click', () => {
16 | console.log("div was clicked")
17 | })
18 |
19 | span.addEventListener('click', () => {
20 | console.log("span was clicked")
21 | })
22 |
23 | button.addEventListener('click', (event) => {
24 | event.stopPropagation()
25 | console.log("button was clicked")
26 | })
--------------------------------------------------------------------------------
/Event-bubbling/Example-2/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Event bubbling
9 |
12 |
13 |
14 |
15 |
16 | Click Me!
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Event-bubbling/Example-2/style.css:
--------------------------------------------------------------------------------
1 | body {
2 | padding: 20px;
3 | background-color: pink;
4 | }
5 |
6 | div {
7 | padding: 20px;
8 | background-color: green;
9 | width: max-content;
10 | }
11 |
12 | span {
13 | display: block;
14 | padding: 20px;
15 | background-color: blue;
16 | }
17 |
--------------------------------------------------------------------------------
/Every/app.js:
--------------------------------------------------------------------------------
1 | //This funtion methods executes a function for each array eleemnt
2 | //function returns True if the function returns true of all elements
3 | //function returns False if the function returns true of all elements
4 | // method dones not executes for empty values
5 | //does not change the array of origional array
6 | const myfriends = [
7 | { name: 'Mamoon', age: 25, position: 'Senior Full stack', salary: 1000 },
8 | { name: 'Mubahsar', age: 26, position: 'developer', salary: 100 },
9 | { name: 'Babar', age: 25, position: 'SQA', salary: 6000 },
10 | { name: 'Haroon', age: 24, position: 'SQA', salary: 400 },
11 | { name: 'Amir', age: 25, position: 'Full stack', salary: 50 },
12 | ];
13 | let result= myfriends.every((friend)=>friend.age>40)
14 | console.log(result)
15 |
--------------------------------------------------------------------------------
/Every/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Javascript Evey Method
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Filter/app.js:
--------------------------------------------------------------------------------
1 | //iterates over an array
2 | //return new array after the complete iteration]
3 | //does not iterates over the array with empty elements
4 | //filter method does not change the origional array
5 | //return empty array when not condition meets
6 |
7 | const foodItems= ['milk','water','sugar','jam'];
8 | let Istrue= foodItems.filter((item)=>{
9 | return item==='meat'
10 | })
11 | console.log(Istrue)
12 |
13 | const isAvailable= foodItems.filter((item)=>{
14 | return item==='milk'
15 | })
16 | console.log(isAvailable)
--------------------------------------------------------------------------------
/Filter/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Javascript Filter Method
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Find/app.js:
--------------------------------------------------------------------------------
1 | // returns a single value, always returns a single ,if no match found return undefined
2 |
3 | const myfriends = [
4 | { name: 'Mamoon', age: 25, position: 'Senior Full stack', salary: 1000 },
5 | { name: 'Mubahsar', age: 26, position: 'developer', salary: 100 },
6 | { name: 'Babar', age: 25, position: 'SQA', salary: 6000 },
7 | { name: 'Haroon', age: 24, position: 'SQA', salary: 400 },
8 | { name: 'Amir', age: 25, position: 'Full stack', salary: 50 },
9 | ];
10 |
11 | let isAvailable= myfriends.find((friend)=>friend.name==='Mamoon')
12 | console.log(isAvailable)
13 |
14 | isAvailable= myfriends.find((friend)=>friend.name==='unce tom')
15 | console.log(isAvailable)
--------------------------------------------------------------------------------
/Find/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Javascript Find Method
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/ForOf/ForIn/app.js:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Amir-ali-anwar/Javascript-nuggets/215e5e886ca7f6417552a3e2a4e6b55270e49fcd/ForOf/ForIn/app.js
--------------------------------------------------------------------------------
/ForOf/ForIn/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Forof /Forin loop in javascript
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Form-Enteries/app.js:
--------------------------------------------------------------------------------
1 | let form = document.querySelector('.form');
2 | form?.addEventListener('submit', (e) => {
3 | e.preventDefault();
4 | const formData = [... new FormData(e.currentTarget).values()];
5 | console.log(formData);
6 | if (formData.includes('')) {
7 | showError("Please enter all the values")
8 | throw new Error("Please enter all the values")
9 | }
10 | const formObject = Object.fromEntries(formData)
11 | let values = []
12 | for (const [key, value] of formObject) {
13 | values.push(`${key}: ${value}`);
14 | }
15 | const message = values.join('\n');
16 | showSuccess({message})
17 | currentTarget.reset();
18 | });
19 | function showError(msg) {
20 | Swal.fire({
21 | icon: 'error',
22 | title: 'Oops...',
23 | text: msg,
24 | })
25 | }
26 |
27 | function showSuccess(msg) {
28 | Swal.fire({
29 | icon: 'Success',
30 | title: 'Your form Values',
31 | text: msg,
32 | })
33 | }
34 |
35 |
36 | // Object.fromEntries()
37 |
38 | //The fromEntries() method creates an object from iterable key / value pairs.
39 |
40 | //1.Converting an Array of Key-Value Pairs to an Object
41 | const fruits = [
42 | ["apples", 300],
43 | ["pears", 900],
44 | ["bananas", 500]
45 | ];
46 |
47 |
48 | const myObj= Object.fromEntries(fruits)
49 | console.log({myObj});
50 |
51 | //2.Converting a Map to an Object
52 |
53 | const map = new Map([
54 | ['name', 'Alice'],
55 | ['age', 30],
56 | ['city', 'New York']
57 | ]);
58 | const obj = Object.fromEntries(map);
59 | console.log({obj});
60 |
61 | //3.Filtering and Modifying Entries Before Converting Back
--------------------------------------------------------------------------------
/Form-Enteries/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Javascript Nuggets
8 |
9 |
10 |
11 |
12 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/Form-Enteries/style.css:
--------------------------------------------------------------------------------
1 | *,
2 | ::after,
3 | ::before {
4 | box-sizing: border-box;
5 | margin: 0;
6 | padding: 0;
7 | }
8 | /* fonts */
9 | @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;600&family=Montserrat&display=swap');
10 |
11 | html {
12 | font-size: 100%;
13 | } /*16px*/
14 |
15 | :root {
16 | /* colors */
17 | --primary-100: #e2e0ff;
18 | --primary-200: #c1beff;
19 | --primary-300: #a29dff;
20 | --primary-400: #837dff;
21 | --primary-500: #645cff;
22 | --primary-600: #504acc;
23 | --primary-700: #3c3799;
24 | --primary-800: #282566;
25 | --primary-900: #141233;
26 |
27 | /* grey */
28 | --grey-50: #f8fafc;
29 | --grey-100: #f1f5f9;
30 | --grey-200: #e2e8f0;
31 | --grey-300: #cbd5e1;
32 | --grey-400: #94a3b8;
33 | --grey-500: #64748b;
34 | --grey-600: #475569;
35 | --grey-700: #334155;
36 | --grey-800: #1e293b;
37 | --grey-900: #0f172a;
38 | /* rest of the colors */
39 | --black: #222;
40 | --white: #fff;
41 | --red-light: #f8d7da;
42 | --red-dark: #842029;
43 | --green-light: #d1e7dd;
44 | --green-dark: #0f5132;
45 |
46 | /* fonts */
47 | --headingFont: 'Roboto', sans-serif;
48 | --bodyFont: 'Nunito', sans-serif;
49 | --small-text: 0.875rem;
50 | --extra-small-text: 0.7em;
51 | /* rest of the vars */
52 | --backgroundColor: var(--grey-50);
53 | --textColor: var(--grey-900);
54 | --borderRadius: 0.25rem;
55 | --letterSpacing: 1px;
56 | --transition: 0.3s ease-in-out all;
57 | --max-width: 1120px;
58 | --fixed-width: 600px;
59 |
60 | /* box shadow*/
61 | --shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
62 | --shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
63 | 0 2px 4px -1px rgba(0, 0, 0, 0.06);
64 | --shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
65 | 0 4px 6px -2px rgba(0, 0, 0, 0.05);
66 | --shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
67 | 0 10px 10px -5px rgba(0, 0, 0, 0.04);
68 | }
69 |
70 | body {
71 | background: var(--backgroundColor);
72 | font-family: var(--bodyFont);
73 | font-weight: 400;
74 | line-height: 1.75;
75 | color: var(--textColor);
76 | }
77 |
78 | p {
79 | margin-bottom: 1.5rem;
80 | max-width: 40em;
81 | }
82 |
83 | h1,
84 | h2,
85 | h3,
86 | h4,
87 | h5 {
88 | margin: 0;
89 | margin-bottom: 1.38rem;
90 | font-family: var(--headingFont);
91 | font-weight: 400;
92 | line-height: 1.3;
93 | text-transform: capitalize;
94 | letter-spacing: var(--letterSpacing);
95 | }
96 |
97 | h1 {
98 | margin-top: 0;
99 | font-size: 3.052rem;
100 | }
101 |
102 | h2 {
103 | font-size: 2.441rem;
104 | }
105 |
106 | h3 {
107 | font-size: 1.953rem;
108 | }
109 |
110 | h4 {
111 | font-size: 1.563rem;
112 | }
113 |
114 | h5 {
115 | font-size: 1.25rem;
116 | }
117 |
118 | small,
119 | .text-small {
120 | font-size: var(--small-text);
121 | }
122 |
123 | a {
124 | text-decoration: none;
125 | }
126 | ul {
127 | list-style-type: none;
128 | padding: 0;
129 | }
130 |
131 | .img {
132 | width: 100%;
133 | display: block;
134 | object-fit: cover;
135 | }
136 | /* buttons */
137 |
138 | .btn {
139 | cursor: pointer;
140 | color: var(--white);
141 | background: var(--primary-500);
142 | border: transparent;
143 | border-radius: var(--borderRadius);
144 | letter-spacing: var(--letterSpacing);
145 | padding: 0.375rem 0.75rem;
146 | box-shadow: var(--shadow-1);
147 | transition: var(--transition);
148 | text-transform: capitalize;
149 | display: inline-block;
150 | }
151 | .btn:hover {
152 | background: var(--primary-700);
153 | box-shadow: var(--shadow-3);
154 | }
155 | .btn-hipster {
156 | color: var(--primary-500);
157 | background: var(--primary-200);
158 | }
159 | .btn-hipster:hover {
160 | color: var(--primary-200);
161 | background: var(--primary-700);
162 | }
163 | .btn-block {
164 | width: 100%;
165 | }
166 |
167 | /* alerts */
168 | .alert {
169 | padding: 0.375rem 0.75rem;
170 | margin-bottom: 1rem;
171 | border-color: transparent;
172 | border-radius: var(--borderRadius);
173 | }
174 |
175 | .alert-danger {
176 | color: var(--red-dark);
177 | background: var(--red-light);
178 | }
179 | .alert-success {
180 | color: var(--green-dark);
181 | background: var(--green-light);
182 | }
183 | /* form */
184 |
185 | .form {
186 | width: 90vw;
187 | max-width: var(--fixed-width);
188 | background: var(--white);
189 | border-radius: var(--borderRadius);
190 | box-shadow: var(--shadow-2);
191 | padding: 2rem 2.5rem;
192 | margin: 3rem auto;
193 | }
194 | .form-label {
195 | display: block;
196 | font-size: var(--small-text);
197 | margin-bottom: 0.5rem;
198 | text-transform: capitalize;
199 | letter-spacing: var(--letterSpacing);
200 | }
201 | .form-input,
202 | .form-textarea {
203 | width: 100%;
204 | padding: 0.375rem 0.75rem;
205 | border-radius: var(--borderRadius);
206 | background: var(--backgroundColor);
207 | border: 1px solid var(--grey-200);
208 | }
209 |
210 | .form-row {
211 | margin-bottom: 1rem;
212 | }
213 |
214 | .form-textarea {
215 | height: 7rem;
216 | }
217 | ::placeholder {
218 | font-family: inherit;
219 | color: var(--grey-400);
220 | }
221 | .form-alert {
222 | color: var(--red-dark);
223 | letter-spacing: var(--letterSpacing);
224 | text-transform: capitalize;
225 | }
226 | /* alert */
227 |
228 | @keyframes spinner {
229 | to {
230 | transform: rotate(360deg);
231 | }
232 | }
233 |
234 | .loading {
235 | width: 6rem;
236 | height: 6rem;
237 | border: 5px solid var(--grey-400);
238 | border-radius: 50%;
239 | border-top-color: var(--primary-500);
240 | animation: spinner 0.6s linear infinite;
241 | }
242 | .loading {
243 | margin: 0 auto;
244 | }
245 | /* title */
246 |
247 | .title {
248 | text-align: center;
249 | }
250 |
251 | .title-underline {
252 | background: var(--primary-500);
253 | width: 7rem;
254 | height: 0.25rem;
255 | margin: 0 auto;
256 | margin-top: -1rem;
257 | }
258 |
259 | .form h4 {
260 | text-align: center;
261 | }
262 | .form button {
263 | margin-top: 0.5rem;
264 | }
265 |
--------------------------------------------------------------------------------
/FormData/app.js:
--------------------------------------------------------------------------------
1 | //target =>refers to the element where clicked
2 | // currentTarget => referes to the element where the listner is added
3 | let isSubmitted = false;
4 | let error=""
5 | const form = document.querySelector('.form');
6 | const formValues = document.querySelector('#form-data');
7 | form.addEventListener('submit', (e) => {
8 | e.preventDefault();
9 | isSubmitted=true;
10 | const formData = new FormData(e.currentTarget);
11 | const formObject = Object.fromEntries(formData);
12 | if (!formObject.name) {
13 | error= "Please enter Name"
14 | console.log('Please enter Name')
15 | }
16 | if (!formObject.email) {
17 | error= "Please enter Email"
18 | console.log('Please enter Email')
19 | if(formObject.email){
20 | if(!isValidEmail(formObject.email)){
21 | console.log('Please enter a valid email address')
22 | // alert('Please enter a valid email address');
23 | }
24 | }
25 | }
26 | if (!formObject.password) {
27 | error= "Please enter Password"
28 | console.log('Please enter Password')
29 | }
30 | e.currentTarget.reset();
31 | })
32 |
33 | if (isSubmitted) {
34 | formValues.style.display = 'block'
35 | } else {
36 | formValues.style.display = 'none'
37 | }
38 |
39 |
40 | function isValidEmail(email) {
41 | const emailRegex = /^\S+@\S+\.\S+$/;
42 | return emailRegex.test(email);
43 | }
44 |
45 |
--------------------------------------------------------------------------------
/FormData/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Form data
9 |
10 |
11 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/FormData/style.css:
--------------------------------------------------------------------------------
1 | *,
2 | ::after,
3 | ::before {
4 | box-sizing: border-box;
5 | margin: 0;
6 | padding: 0;
7 | }
8 | /* fonts */
9 | @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;600&family=Montserrat&display=swap');
10 |
11 | html {
12 | font-size: 100%;
13 | } /*16px*/
14 |
15 | :root {
16 | /* colors */
17 | --primary-100: #e2e0ff;
18 | --primary-200: #c1beff;
19 | --primary-300: #a29dff;
20 | --primary-400: #837dff;
21 | --primary-500: #645cff;
22 | --primary-600: #504acc;
23 | --primary-700: #3c3799;
24 | --primary-800: #282566;
25 | --primary-900: #141233;
26 |
27 | /* grey */
28 | --grey-50: #f8fafc;
29 | --grey-100: #f1f5f9;
30 | --grey-200: #e2e8f0;
31 | --grey-300: #cbd5e1;
32 | --grey-400: #94a3b8;
33 | --grey-500: #64748b;
34 | --grey-600: #475569;
35 | --grey-700: #334155;
36 | --grey-800: #1e293b;
37 | --grey-900: #0f172a;
38 | /* rest of the colors */
39 | --black: #222;
40 | --white: #fff;
41 | --red-light: #f8d7da;
42 | --red-dark: #842029;
43 | --green-light: #d1e7dd;
44 | --green-dark: #0f5132;
45 |
46 | /* fonts */
47 | --headingFont: 'Roboto', sans-serif;
48 | --bodyFont: 'Nunito', sans-serif;
49 | --small-text: 0.875rem;
50 | --extra-small-text: 0.7em;
51 | /* rest of the vars */
52 | --backgroundColor: var(--grey-50);
53 | --textColor: var(--grey-900);
54 | --borderRadius: 0.25rem;
55 | --letterSpacing: 1px;
56 | --transition: 0.3s ease-in-out all;
57 | --max-width: 1120px;
58 | --fixed-width: 600px;
59 |
60 | /* box shadow*/
61 | --shadow-1: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
62 | --shadow-2: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
63 | 0 2px 4px -1px rgba(0, 0, 0, 0.06);
64 | --shadow-3: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
65 | 0 4px 6px -2px rgba(0, 0, 0, 0.05);
66 | --shadow-4: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
67 | 0 10px 10px -5px rgba(0, 0, 0, 0.04);
68 | }
69 |
70 | body {
71 | background: var(--backgroundColor);
72 | font-family: var(--bodyFont);
73 | font-weight: 400;
74 | line-height: 1.75;
75 | color: var(--textColor);
76 | }
77 |
78 | p {
79 | margin-bottom: 1.5rem;
80 | max-width: 40em;
81 | }
82 |
83 | h1,
84 | h2,
85 | h3,
86 | h4,
87 | h5 {
88 | margin: 0;
89 | margin-bottom: 1.38rem;
90 | font-family: var(--headingFont);
91 | font-weight: 400;
92 | line-height: 1.3;
93 | text-transform: capitalize;
94 | letter-spacing: var(--letterSpacing);
95 | }
96 |
97 | h1 {
98 | margin-top: 0;
99 | font-size: 3.052rem;
100 | }
101 |
102 | h2 {
103 | font-size: 2.441rem;
104 | }
105 |
106 | h3 {
107 | font-size: 1.953rem;
108 | }
109 |
110 | h4 {
111 | font-size: 1.563rem;
112 | }
113 |
114 | h5 {
115 | font-size: 1.25rem;
116 | }
117 |
118 | small,
119 | .text-small {
120 | font-size: var(--small-text);
121 | }
122 |
123 | a {
124 | text-decoration: none;
125 | }
126 | ul {
127 | list-style-type: none;
128 | padding: 0;
129 | }
130 |
131 | .img {
132 | width: 100%;
133 | display: block;
134 | object-fit: cover;
135 | }
136 | /* buttons */
137 |
138 | .btn {
139 | cursor: pointer;
140 | color: var(--white);
141 | background: var(--primary-500);
142 | border: transparent;
143 | border-radius: var(--borderRadius);
144 | letter-spacing: var(--letterSpacing);
145 | padding: 0.375rem 0.75rem;
146 | box-shadow: var(--shadow-1);
147 | transition: var(--transition);
148 | text-transform: capitalize;
149 | display: inline-block;
150 | }
151 | .btn:hover {
152 | background: var(--primary-700);
153 | box-shadow: var(--shadow-3);
154 | }
155 | .btn-hipster {
156 | color: var(--primary-500);
157 | background: var(--primary-200);
158 | }
159 | .btn-hipster:hover {
160 | color: var(--primary-200);
161 | background: var(--primary-700);
162 | }
163 | .btn-block {
164 | width: 100%;
165 | }
166 |
167 | /* alerts */
168 | .alert {
169 | padding: 0.375rem 0.75rem;
170 | margin-bottom: 1rem;
171 | border-color: transparent;
172 | border-radius: var(--borderRadius);
173 | }
174 |
175 | .alert-danger {
176 | color: var(--red-dark);
177 | background: var(--red-light);
178 | }
179 | .alert-success {
180 | color: var(--green-dark);
181 | background: var(--green-light);
182 | }
183 | /* form */
184 |
185 | .form {
186 | width: 90vw;
187 | max-width: var(--fixed-width);
188 | background: var(--white);
189 | border-radius: var(--borderRadius);
190 | box-shadow: var(--shadow-2);
191 | padding: 2rem 2.5rem;
192 | margin: 3rem auto;
193 | }
194 | .form-label {
195 | display: block;
196 | font-size: var(--small-text);
197 | margin-bottom: 0.5rem;
198 | text-transform: capitalize;
199 | letter-spacing: var(--letterSpacing);
200 | }
201 | .form-input,
202 | .form-textarea {
203 | width: 100%;
204 | padding: 0.375rem 0.75rem;
205 | border-radius: var(--borderRadius);
206 | background: var(--backgroundColor);
207 | border: 1px solid var(--grey-200);
208 | }
209 |
210 | .form-row {
211 | margin-bottom: 1rem;
212 | }
213 |
214 | .form-textarea {
215 | height: 7rem;
216 | }
217 | ::placeholder {
218 | font-family: inherit;
219 | color: var(--grey-400);
220 | }
221 | .form-alert {
222 | color: var(--red-dark);
223 | letter-spacing: var(--letterSpacing);
224 | text-transform: capitalize;
225 | }
226 | /* alert */
227 |
228 | @keyframes spinner {
229 | to {
230 | transform: rotate(360deg);
231 | }
232 | }
233 |
234 | .loading {
235 | width: 6rem;
236 | height: 6rem;
237 | border: 5px solid var(--grey-400);
238 | border-radius: 50%;
239 | border-top-color: var(--primary-500);
240 | animation: spinner 0.6s linear infinite;
241 | }
242 | .loading {
243 | margin: 0 auto;
244 | }
245 | /* title */
246 |
247 | .title {
248 | text-align: center;
249 | }
250 |
251 | .title-underline {
252 | background: var(--primary-500);
253 | width: 7rem;
254 | height: 0.25rem;
255 | margin: 0 auto;
256 | margin-top: -1rem;
257 | }
258 |
259 | .form h4 {
260 | text-align: center;
261 | }
262 | .form button {
263 | margin-top: 0.5rem;
264 | }
--------------------------------------------------------------------------------
/Functions/index.js:
--------------------------------------------------------------------------------
1 | function add(a, b) {
2 | if (new.target) {
3 | return 'The add function cannot be called as a constructor';
4 | }
5 | return a + b
6 | }
7 | console.log(add.length);
8 | console.log(add.prototype);
9 |
10 | let result = new add(10, 30)
11 | console.log(result);
12 |
13 |
14 | // closure
15 |
16 | function outer() {
17 | this.value = 42;
18 | function inner() {
19 | console.log(this.value);
20 | return this.value
21 | }
22 | return inner()
23 | }
24 |
25 | const closureFunc = outer();
26 | console.log(closureFunc);
27 | // closureFunc(); // Outputs: undefined (or an error in strict mode)
28 |
--------------------------------------------------------------------------------
/GeneratorFunctions/app.js:
--------------------------------------------------------------------------------
1 | // A generator function in JavaScript is a special type of function that can be paused and resumed during its execution.
2 | //It is defined using the function* syntax, and it returns a generator object which conforms to both the iterable and iterator protocols.
3 | // The yield keyword is used to pause the function and return a value.
4 | // When the generator's next() method is called, it resumes execution until it either hits another yield statement or completes.
5 |
6 | function* generatorFuntion() {
7 | yield "First output";
8 | yield "Second Output";
9 | yield "Third Output";
10 | }
11 |
12 | const gen = generatorFuntion();
13 | console.log(gen.next());
14 | console.log(gen.next());
15 | console.log(gen.next());
16 | console.log(gen.next());
17 |
18 | // Second Example
19 |
20 | function* RangeGenerator(start, end) {
21 | for (let i = start; i <= end; i++) {
22 | yield i;
23 | }
24 | }
25 |
26 | const range = RangeGenerator(1, 10);
27 | for (const rangeNumber of range) {
28 | console.log({ rangeNumber });
29 | }
30 |
31 |
32 | // . Delegating Generators:
33 |
34 | // A generator can delegate to another generator using yield*.
35 |
36 | function* innerGenerator(){
37 | yield 'Hello',
38 | yield 'from'
39 | }
40 |
41 | function* outerGenerator(){
42 | yield* innerGenerator()
43 | yield 'the outside!';
44 | }
45 |
46 | const gen1 = outerGenerator();
47 | console.log(gen1.next());
48 | console.log(gen1.next());
49 | console.log(gen1.next());
50 |
51 |
--------------------------------------------------------------------------------
/GeneratorFunctions/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Generator function
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/High-Order-Functions/app.js:
--------------------------------------------------------------------------------
1 | // passing function as a parameter to the another is called callback, and the function that is receving the function as a paramater is called
2 | // higher oder function
3 |
4 |
5 | function goodfriend(name) {
6 | return `${name}`
7 | }
8 | function visitor(name) {
9 | return `${name}`
10 | }
11 | function greatfriends(name, cbFn) {
12 | console.log(`Hi ${cbFn(name)} and welcome to this Higher order funtion tutorial`)
13 | }
14 |
15 | // greatfriends('amir', goodfriend)
16 | greatfriends('mamoon', visitor)
17 |
--------------------------------------------------------------------------------
/High-Order-Functions/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | High Order Function in javascript
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Image-Magnifer/app.js:
--------------------------------------------------------------------------------
1 | const formSubmit = document.querySelector('.submit--form');
2 | const image = document.querySelector('#fileInput');
3 | const imagePreview = document.querySelector('#image-preview');
4 | const Noimg = document.querySelector('#image-preview p');
5 | const magnifier = document.getElementById('magnifier');
6 |
7 | function showMagnifier() {
8 | magnifier.style.display = 'block';
9 | }
10 |
11 | function hideMagnifier() {
12 | magnifier.style.display = 'none';
13 | }
14 |
15 | formSubmit.addEventListener('submit', (event) => {
16 | event.preventDefault();
17 | if (image.files.length > 0) {
18 | const file = image.files[0]; // Get the first file
19 | const maxFileSizeInKB = 1024;
20 |
21 | // Check if the file size exceeds the limit
22 | if (file.size > maxFileSizeInKB * 1024) {
23 | alert("File size exceeds " + maxFileSizeInKB + "KB. Please choose a smaller file.");
24 | return;
25 | }
26 | let fileReader = new FileReader();
27 | const imgElement = new Image();
28 | fileReader.onload = (e) => {
29 | const imageSrc = e.target.result;
30 | imgElement.src = imageSrc;
31 | imgElement.style.maxWidth = '100%';
32 | imgElement.style.height = '400px';
33 | imgElement.style.objectFit = 'contain';
34 | // Remove previous image if exists
35 | while (imagePreview.firstChild) {
36 | imagePreview.removeChild(imagePreview.firstChild);
37 | }
38 | imagePreview.appendChild(imgElement);
39 | Noimg.classList.add('no-img');
40 |
41 | // Attach event listeners to the newly added imgElement
42 | imgElement.addEventListener('mouseenter', showMagnifier);
43 | imgElement.addEventListener('mouseleave', hideMagnifier);
44 | imgElement.addEventListener('mousemove', handleMouseEnter);
45 | };
46 | fileReader.readAsDataURL(file);
47 | }
48 | formSubmit.reset();
49 | });
50 | function handleMouseEnter(event) {
51 | const imgElement = event.target;
52 | const rect = imgElement.getBoundingClientRect();
53 | const clientX = event.clientX - rect.left;
54 | const clientY = event.clientY - rect.top;
55 |
56 | // Calculate background position for magnifier
57 | const bgPosX = -clientX * 2 + magnifier.offsetWidth / 2;
58 | const bgPosY = -clientY * 2 + magnifier.offsetHeight / 2;
59 |
60 | // Position and update magnifier
61 | magnifier.style.left = event.pageX + 'px';
62 | magnifier.style.top = event.pageY + 'px';
63 | magnifier.style.backgroundPosition = `${bgPosX}px ${bgPosY}px`;
64 | magnifier.style.backgroundImage = `url('${imgElement.src}')`;
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/Image-Magnifer/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | File upload
7 |
9 |
10 |
11 |
12 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
58 |
81 |
85 |
86 |
87 |
90 |
91 |
92 |
93 |
94 |
No Image Picked yet.
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
--------------------------------------------------------------------------------
/Image-Magnifer/style.css:
--------------------------------------------------------------------------------
1 | *, *:after, *:before {
2 | box-sizing: border-box;
3 | }
4 |
5 | :root {
6 | --c-action-primary: #2e44ff;
7 | --c-action-primary-accent: #e9e5ff;
8 | --c-action-secondary: #e5e5e5;
9 | --c-text-primary: #0d0f21;
10 | --c-text-secondary: #6a6b76;
11 | --c-background-primary: #d0d1de;
12 | }
13 |
14 | body {
15 | font-family: "Inter", sans-serif;
16 | color: var(--c-text-primary);
17 | background-color: var(--c-background-primary);
18 | line-height: 1.5;
19 | }
20 |
21 | input, button, select, textarea {
22 | font: inherit;
23 | }
24 |
25 | .modal {
26 | width: 90%;
27 | max-width: 500px;
28 | margin-left: auto;
29 | margin-right: auto;
30 | margin-top: 10vh;
31 | margin-bottom: 10vh;
32 | background-color: #FFF;
33 | border-radius: 0.5rem;
34 | box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
35 | }
36 |
37 | .modal-header {
38 | display: flex;
39 | align-items: flex-start;
40 | justify-content: space-between;
41 | padding: 1.5rem 1.5rem 1rem;
42 | }
43 |
44 | .logo-circle {
45 | width: 3.5rem;
46 | height: 3.5rem;
47 | display: flex;
48 | justify-content: center;
49 | align-items: center;
50 | border-radius: 50%;
51 | background-color: var(--c-action-primary-accent);
52 | }
53 | .logo-circle svg {
54 | max-width: 1.5rem;
55 | }
56 |
57 | .btn-close {
58 | display: flex;
59 | align-items: center;
60 | justify-content: center;
61 | width: 2.25rem;
62 | height: 2.25rem;
63 | border-radius: 0.25rem;
64 | border: none;
65 | background-color: transparent;
66 | }
67 | .btn-close:hover, .btn-close:focus {
68 | background-color: var(--c-action-primary-accent);
69 | }
70 |
71 | .modal-body {
72 | padding: 1rem 1.5rem;
73 | }
74 |
75 | .modal-title {
76 | font-weight: 700;
77 | }
78 |
79 | .modal-description {
80 | color: var(--c-text-secondary);
81 | }
82 |
83 | .upload-area {
84 | margin-top: 1.25rem;
85 | border: none;
86 | background-image: url("data:image/svg+xml,%3csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3e%3crect width='100%25' height='100%25' fill='none' stroke='%23ccc' stroke-width='3' stroke-dasharray='6%2c 14' stroke-dashoffset='0' stroke-linecap='square'/%3e%3c/svg%3e");
87 | background-color: transparent;
88 | padding: 3rem;
89 | width: 100%;
90 | display: flex;
91 | flex-direction: column;
92 | align-items: center;
93 | }
94 | .upload-area:hover, .upload-area:focus {
95 | background-image: url("data:image/svg+xml,%3csvg width='100%25' height='100%25' xmlns='http://www.w3.org/2000/svg'%3e%3crect width='100%25' height='100%25' fill='none' stroke='%232e44ff' stroke-width='3' stroke-dasharray='6%2c 14' stroke-dashoffset='0' stroke-linecap='square'/%3e%3c/svg%3e");
96 | }
97 |
98 | .upload-area-icon {
99 | display: block;
100 | width: 2.25rem;
101 | height: 2.25rem;
102 | }
103 | .upload-area-icon svg {
104 | max-height: 100%;
105 | max-width: 100%;
106 | }
107 |
108 | .upload-area-title {
109 | margin-top: 1rem;
110 | display: block;
111 | font-weight: 700;
112 | color: var(--c-text-primary);
113 | }
114 | .no-img {
115 | display: none;
116 | }
117 | .upload-area-description {
118 | display: block;
119 | text-align: center;
120 | color: var(--c-text-secondary);
121 | }
122 | .upload-area-description input,
123 | .upload-area-description strong {
124 | color: var(--c-action-primary);
125 | font-weight: 700;
126 | }
127 |
128 | .modal-footer {
129 | padding: 1rem 1.5rem 1.5rem;
130 | display: flex;
131 | justify-content: flex-end;
132 | }
133 | .modal-footer [class*=btn-] {
134 | margin-left: 0.75rem;
135 | }
136 |
137 | .btn-secondary, .btn-primary {
138 | padding: 0.5rem 1rem;
139 | font-weight: 500;
140 | border: 2px solid var(--c-action-secondary);
141 | border-radius: 0.25rem;
142 | background-color: transparent;
143 | }
144 |
145 | .btn-primary {
146 | color: #FFF;
147 | background-color: var(--c-action-primary);
148 | border-color: var(--c-action-primary);
149 | }
150 | .magnifer-main {
151 | display: flex;
152 | align-items: center;
153 | max-width: 1200px;
154 | margin: 20px auto;
155 | gap: 0 50px;
156 | justify-content: space-between;
157 | }
158 |
159 | #image-container {
160 | position: relative;
161 | width: fit-content; /* Adjust according to your layout */
162 | }
163 |
164 | #magnifier {
165 | position: absolute;
166 | border: 1px solid #ccc;
167 | background-color: rgba(255, 255, 255, 0.5);
168 | width: 200px; /* Adjust the size of the magnifier */
169 | height: 200px; /* Adjust the size of the magnifier */
170 | display: none;
171 | pointer-events: none; /* Ensure the magnifier doesn't interfere with mouse events */
172 | /* Additional styling for magnifier */
173 | }
174 |
--------------------------------------------------------------------------------
/Image-Preview/app.js:
--------------------------------------------------------------------------------
1 | const formSubmit = document.querySelector('.submit--form');
2 | const image = document.querySelector('#fileInput');
3 | const imagePreview = document.querySelector('#image-preview');
4 | const Noimg = document.querySelector('#image-preview p');
5 |
6 | formSubmit.addEventListener('submit', (event) => {
7 | event.preventDefault();
8 |
9 | const previousImage = imagePreview.querySelector('img');
10 | if (previousImage) {
11 | imagePreview.removeChild(previousImage);
12 | Noimg.classList.remove('no-img');
13 | }
14 |
15 | if (image.files.length > 0) {
16 | const file = image.files[0]; // Get the first file
17 | const maxFileSizeInKB = 500;
18 |
19 | // Check if the file size exceeds the limit
20 | if (file.size > maxFileSizeInKB * 1024) {
21 | alert("File size exceeds " + maxFileSizeInKB + "KB. Please choose a smaller file.");
22 | return;
23 | }
24 |
25 | let fileReader = new FileReader();
26 | const imgElement = new Image();
27 | fileReader.onload = (e) => {
28 | const imageSrc = e.target.result;
29 | imgElement.src = imageSrc;
30 | imgElement.style.maxWidth = '100%';
31 | imgElement.style.height = '400px';
32 | imgElement.style.objectFit = 'contain';
33 | imagePreview.appendChild(imgElement);
34 | Noimg.classList.add('no-img');
35 | };
36 | fileReader.readAsDataURL(file);
37 | }
38 | formSubmit.reset();
39 | });
40 |
--------------------------------------------------------------------------------
/Image-Preview/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |