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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions fatemeh/JS/Basic_Syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// add number a to b
function add(a, b) {
return a + b;
}

// flip a coin and log the result (head or tail)
function flipACoin() {
let num = Math.floor(Math.random() * 2);

switch (num) {
case 0:
return "head";
case 1:
return "tail";
}
}

// concat two strings
function concat(stringA, stringB) {
return stringA.concat(stringB);
}

// 2^x = ?
function power2(x) {
return x * x;
}

// user object with first name and last name, both capitalized
// e.g. getUser('hello', 'world') is { firstName: 'Hello', lastName: 'World' }
function getUser(firstName, lastName) {
const newFirstName = firstName
.slice(0, 1)
.toUpperCase()
.concat(firstName.slice(1).toLowerCase());
const newLastName = lastName
.slice(0, 1)
.toUpperCase()
.concat(lastName.slice(1).toLowerCase());

return newFirstName, newLastName;
}

// greet someone!
// e.g. greet('ConnecMent') is "Hello, ConnecMent! Hope you the best."
function greet(name) {
return "Hello, ".concat(name, "! Hope you the best.");
}

// get array of n random numbers
function getNRandomNumbers(n) {
const resultArr = [];
for (let i = 0; i < n; i++) {
resultArr[i] = Math.floor(Math.random() * 100);
}

return resultArr;
}

// get sorted array of n random numbers, in range min to max
function getNRandomNumbers(n, min, max) {
const resultArr = [];
for (let i = 0; i < n; i++) {
resultArr[i] = Math.floor(Math.random() * (max - min) + min);
}

return resultArr;
}

// log current time with a x seconds interval, for n seconds
function logCurrentTime(x, n) {
let counter = 0;
let intervalID = setInterval(() => {
if (counter == n) {
clearInterval(intervalID);
} else {
console.log(Date());
counter += 1;
}
}, x * 1000); //this sets the speed of the animation
}

// higher order function: call the function in the parameters list
// e.g. callFuncNTimes(flipACoin, 3) calls flipACoin 3 times
function callFuncNTimes(func, n) {
for (let i = 0; i < n; i++) {
func(n);
}
}

// return true if parameter is false, false otherwise
function isNil(param) {
if (param == true) {
return true;
} else {
return false;
}
}

// call property in object
// e.g. in obj = { callableProp: function () { console.log('hello!') } }, callPropInObj(obj, 'callableProp') will log "hello!"
function callPropInObj(object, propertyName) {
object[propertyName]();
}

// if the email is gmail
function isGmail(email) {
if (email.match(/@gmail.com/i) == null) {
return false;
} else {
return true;
}
}

// duplicate array
// e.g. duplicateArray([1,2,3]) is [1,2,3,1,2,3]
function duplicateArray(Arr) {
let n = Arr.length;
for (let i = 0; i < n; i++) {
Arr.push(Arr[i]);
}
}
52 changes: 52 additions & 0 deletions fatemeh/JS/JS: data fetching + DOM manipulation/addpost.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="addpoststyle.css" />
</head>
<body>
<div class="newPost">
<form name="newPost" method="post">
Title:<br />
<input class="Title" type="text" name="title" /><br />
Author (ID):<br />
<input class="Author" type="text" name="author" /> <br />
Description:<br />
<textarea class="PostBody" type="text" name="postbody"></textarea>
</form>
<div class="buttons">
<button class="addPost" onclick="addNewPost()">+ Add Post</button>
<button class="CancelB" onclick="goBack()">Cancel</button>
</div>
</div>
<script>
function addNewPost() {
document.getElementsByClassName("addPost")[0].innerHTML = "Pending...";

let newTitle = document.forms["newPost"]["title"].value;
let newAuthor = parseInt(document.forms["newPost"]["author"].value);
let newBody = document.forms["newPost"]["postbody"].value;

fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
body: JSON.stringify({
title: newTitle,
body: newBody,
userId: newAuthor,
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.then((json) => Done());
}
function Done() {
document.getElementsByClassName("addPost")[0].innerHTML = "Done!";
location.replace("./index.html");
}
function goBack() {
if (confirm("Are you sure?")) location.replace("./index.html");
}
</script>
</body>
</html>
97 changes: 97 additions & 0 deletions fatemeh/JS/JS: data fetching + DOM manipulation/addpoststyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
.newPost {
border-radius: 20px;
padding: 20px;
background-color: #8bc6ec;
background-image: linear-gradient(135deg, #8bc6ec 0%, #9599e2 100%);
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
display: flex;
flex-direction: column;
justify-items: center;
align-items: center;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
* {
font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
align-items: center;
}
form {
font-weight: bold;
font-size: 20px;
color: #4158d0;
}

button:hover {
box-shadow: 0 0 10px 5px white;
}
button:active {
background-color: #4159d08c;
}
.Title {
font-size: large;
padding: 5px;
margin-top: 5px;
margin-bottom: 1rem;
border: none;
border-radius: 10px;
background-color: rgba(255, 255, 255, 0.6);
}
.Author {
font-size: large;
padding: 5px;
margin-top: 5px;
margin-bottom: 1rem;
border: none;
border-radius: 10px;
background-color: rgba(255, 255, 255, 0.6);
}
.PostBody {
font-size: large;
padding: 5px;
width: 95.5%;
margin-top: 5px;
margin-bottom: 1rem;
height: 5rem;
border: none;
border-radius: 10px;

background-color: rgba(255, 255, 255, 0.6);
}
.addPost {
border: none;
padding: 10px 15px;
border-radius: 10px;
background-color: #4158d0;
font-weight: bold;
color: white;
font-size: 15px;
margin: 0.5rem;
width: 7rem;
}
.CancelB {
border: none;
margin-left: 10px;
padding: 10px 15px;
border-radius: 10px;
background-color: #b721ff;
font-weight: bold;
color: white;
font-size: 15px;
margin: 0.5rem;
width: 7rem;
}
.addPost:hover {
box-shadow: 0 0 10px 5px #aeceff;
}
.addPost:active {
background-color: #4159d073;
}
.CancelB:hover {
box-shadow: 0 0 10px 5px #db8fff;
}
.CancelB:active {
background-color: #b921ff5b;
}
Loading