Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/amreshpro/jscamp
https://github.com/amreshpro/jscamp
Last synced: about 9 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/amreshpro/jscamp
- Owner: amreshpro
- Created: 2023-09-06T17:26:45.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-09-10T06:39:59.000Z (about 1 year ago)
- Last Synced: 2023-09-10T07:29:11.800Z (about 1 year ago)
- Language: JavaScript
- Size: 362 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Assignment-1
```index.html ```
!["html"](screenshots/html.png)## Array Operation
### 1. Create an empty array called "fruit"
```js
let fruits = [] //empty array
console.log(fruits)//[]
```### 2. Add the following fruit to the "fruits" array: "apple", "banana", "orange".
```js
//way-1
fruits.push("apple")
fruits.push("banana")
fruits.push("orange")
//way-2
//fruits.push("apple","banana","orange")
console.log(fruits); // ["apple","banana","orange"]```
### 3. Remove the first fruit from the array
```js
fruits.splice(0,1) // splice(index,how many from that index is deleted)
console.log(fruits)//[ 'banana', 'orange' ]```
### 4. Add "grape" to the end of the array
```js
fruits.push("grape")
console.log(fruits) //[ 'banana', 'orange', 'grape' ]
```### 5. Update the second fruit in the array to "pear".
```js
fruits.splice(1,1,"pear"); //splice(index,howmany,what add in that place)
console.log(fruits) //[ 'banana', 'pear', 'grape' ]
```### 6. Print the final "fruits" array after performing the above operations.
```js
console.log(fruits);//[ 'banana', 'pear', 'grape' ]
```## Objects
### 1.Create an empty object called "person"
```js
let person = {} //empty object
console.log(person)//{}
```### 2.Add the following property from the "person" object: name : "John", age:30, city:"New York"
```js
// way-1
person.name = "John"
person.age=30
person.city= "New York"//way-2
// person={
// name:"John",
// age:30,
// city:"New York"
// }console.log(person)//{ name: 'John', age: 30, city: 'New York' }
```
### 3. Remove the age property from the "person" object
```js
delete person.age //delete the age property from person
console.log(person) //{ name: 'John', city: 'New York' }```
### 4. Add a new property called "job" with the value "Engineer" to the "person" object```js
person.job = "Engineer"
console.log(person) //{ name: 'John', city: 'New York', job: 'Engineer' }```
### 5. Update the "city" propert of the person object to "San Francisco".```js
person.city = "San Francisco"
console.log(person)//{ name: 'John', city: 'San Francisco', job: 'Engineer' }
```### 6. Print the final 'person' object after performing the above operations.
```js
console.log(person) //{ name: 'John', city: 'San Francisco', job: 'Engineer' }
```## ArrayOfObjects
### 1. Create a array of an objects.
```js
let cars = [] //empty array
console.log(cars) //[]
```
### 2.Add three car objects to the "cars" array.Each car object should have the following properties: make:"Toyota",model:"Camry",year:2018```js
cars.push({
make: "Toyota",
model: "Camry",
year: 2018
},
{
make: "Toyota",
model: "Camry",
year: 2018
},{
make: "Toyota",
model: "Camry",
year: 2018
}
)console.log(cars)//[{ make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Camry', year: 2018 }]
```### 3. Remove the first car object from the "cars" array.
```js
cars.splice(0,1)//delete the first car object
console.log(cars)//[ { make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Camry', year: 2018 }]
```### 4. Add a new car object to the "cars" array with the following properties: make:"Honda",model:"Civic",year:2020
```js
cars.push({
make:"Honda",
model:"Civic",
year:2020
})
console.log(cars)//[{ make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Honda', model: 'Civic', year: 2020 }]
```### 5.Update the "model" property of the second car object in the array to "Accord".
```js
cars[1].model ="Accord"//update model at 2nd car
console.log(cars)//[ { make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Accord', year: 2018 },{ make: 'Honda', model: 'Civic', year: 2020 }]
```### 6. Print the final "cars" array after performing the above operations.
```js
console.log(cars)//[ { make: 'Toyota', model: 'Camry', year: 2018 },{ make: 'Toyota', model: 'Accord', year: 2018 },{ make: 'Honda', model: 'Civic', year: 2020 }]
```## Assignment 2
### 1. Solution
```js
let n = 9;
function checkNumber(num){
if(n==0) return "zero"
if(n>0) return "positive"
if(n<0) return "negative"
}const result = checkNumber(n);
console.log(`Entered Number is ${result}`)
```### 2. Solution
```js
let n = 5
function findFactorial(num) {
if (num === 0) return 1;
let fact = 1;
for (let i = 1; i <=num; i++) {
fact = fact * i;
}
return fact;
}const result = findFactorial(n)//5->120
console.log(result)//120
```
### 3. Solution
```js
let a=43,b=12;function largestNumber(a,b){
return a>b ? a :b;}
console.log(largestNumber(a,b))//12
```### 4. Solution
```js
let isPalindromeString = "gfg" // "gfg", "lol"
function checkPalindromeString(str){
if (str === str.split("").reverse().join("")) return true
return false
}console.log(checkPalindromeString(isPalindromeString))//true
```### 5.Solution
```js
let number = 35function isPrimeNumber(num) {
if (num === 2) return true;
if (num % 2 === 0) return false;
for (let i = 2; i < num; i++) {
if (num % i == 0) return false
}
return true}
function printAllPrimeNumberUptoN(n){
for(let i =2;i 1,2,4,7,14, 28(exclude)
// 1+2+4+7+14 =28function isPerfectNumber(num){
let factorSum = 0;
for(let i = 1;i
Change Background Color
Change Color
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container{
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-items: center;
justify-content: center;
align-items: center;
gap: 4px;}
.box{
display: flex;
flex-direction: column;
justify-items: center;
justify-content: center;
align-items: center;
gap: 4px;
}
.btn {
background-color: #b0b0fa;
color: #fff;
padding: 5px 8px 5px 8px;
border-radius: 10px;
font-size: 20px;
width: fit-content;
outline: none;
border: none;
border:2px solid #6f6fc2;
}
.btn:hover{
background-color: #6f6fc2;
}#hexbox{
outline: none;
}
```
```sh
script.js
``````js
const container = document.querySelector('.container');
const hexbox = document.getElementById('hexbox');const color = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
// random color generator
function getRandomColor() {
const pt1 = color[Math.floor(Math.random() * 16)];
const pt2 = color[Math.floor(Math.random() * 16)];
const pt3 = color[Math.floor(Math.random() * 16)];
const pt4 = color[Math.floor(Math.random() * 16)];
const pt5 = color[Math.floor(Math.random() * 16)];
const pt6 = color[Math.floor(Math.random() * 16)];const randomColor = `#${pt1}${pt2}${pt3}${pt4}${pt5}${pt6}`
hexbox.value = randomColor
return randomColor}
// background color
function changeBackgroundColor() {
container.style.backgroundColor = getRandomColor()}
changeBackgroundColor()
//copy
function copyTextContent() {// Select the text field
hexbox.select();
hexbox.setSelectionRange(0, 99999); // For mobile devices// Copy the text inside the text field
navigator.clipboard.writeText(hexbox.value);}
//on click anywhere in body, change the color
container.addEventListener('click', () => {
changeBackgroundColor()
hexbox.addEventListener('hover', copyTextContent())
console.log("body listener")
});```
### 2.Display Names
!["display-names"](screenshots/displayNames.png)
```sh
index.html
``````html
Display Names
Previous
Next
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
width: 100vw;
height: 100vh;
background-color: cadetblue;
display: flex;
flex-direction: column;
justify-items: center;
justify-content: center;
align-items: center;
gap: 4px;
}.box {
display: flex;
flex-direction: row;
justify-items: center;
justify-content: center;
align-items: center;
gap: 4px;
}.btn {
background-color: #b0b0fa;
color: #fff;
padding: 5px 8px 5px 8px;
border-radius: 10px;
font-size: 20px;
width: fit-content;
outline: none;
border: none;
border: 2px solid #6f6fc2;
}
.btn:hover {
background-color: #6f6fc2;
}
```
```sh
script.js
``````js
const showName = document.getElementById("name");
const prev = document.getElementById("prev");
const next = document.getElementById("next");const names = [
"Aarav",
"Sanya",
"Vikram",
"Neha",
"Rohan",
"Priya",
"Amit",
"Meera",
"Rajiv",
"Ananya",
];
console.log(names);//initial name on load
showName.innerText = names[0];let currentState = {
value: 0,
};//next fun
const showNextName = () => {
if (currentState.value === names.length - 1) currentState.value = -1;showName.innerText = names[++currentState.value];
};//prev fun
const showPreviousName = () => {
if (currentState.value === 0) currentState.value = names.length;showName.innerText = names[--currentState.value];
};```