Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suva js day2 activity2 #68

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions Activity/activity1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 2px solid blueviolet;
border-radius: 4px;

}
#result{
color:red;
}

46 changes: 46 additions & 0 deletions Activity/activity1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>
<head>
<title>Activity 1</title>
<link rel="stylesheet" href="activity1.css"/>
</head>
<body>
<form>
<h2>Guess a number that matches with computer</h2>
<h3 id="result"></h3>
<p>Number of trials:</p>
<p id="trials"></p>
<input id="guess" type="text" onchange="guessNumber()"/>
<script>
let x = Math.floor((Math.random() * 10) + 1);
let count=0;
document.write("Computer Guess: "+x)
function guessNumber(){
count++;
let num=document.getElementById("guess").value;
let result=document.getElementById("result")

console.log(x)
console.log(num)
if(num < x){
result.innerHTML="LOWER THAN VALUE"
}
else if(num > x){
result.innerHTML="HIGHER THAN VALUE"

}
else{
result.innerHTML="CORRECT"
result.style.color='green'
}
document.getElementById("trials").innerHTML =count

}

</script>

</form>
</body>

</html>


14 changes: 14 additions & 0 deletions Activity2/fetch_products.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#products {
display: grid;
grid-template-columns: auto auto auto auto;
gap: 1px;
padding: 2px;
}

#products > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 2px;
font-size: 15px;
}

14 changes: 14 additions & 0 deletions Activity2/fetch_products.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<title>
Fetch API
</title>
<link rel="stylesheet" href="fetch_products.css"/>
</head>
<body>
<script src="fetch_products.js"></script>
<h2>Data</h2>
<div id="products" style="display:grid;"></div>
<button id="btnnext">Next</button>
</body>
</html>
34 changes: 34 additions & 0 deletions Activity2/fetch_products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
async function getProducts(){
const url= "https://fakestoreapi.com/products"

try{
let response = await fetch(url);
return await response.json();
}catch(error){
console.log(error)
}
}
async function renderProducts() {
let products = await getProducts();
let html = '';
let i=0
products.forEach(product => {
if(i<10){
let htmlSegment = `<div style="height:500px;width:500px;border-style:solid;">
<h2>${product.id} ${product.title}</h2>

<img src="${product.image}" style="width:200px;height:200px;">

<p>Price:${product.price}</p>
<p>Description:${product.description}</p>
</div>`;

html += htmlSegment;
i++;
}
});

document.getElementById("products").innerHTML = html;
}

renderProducts();
7 changes: 7 additions & 0 deletions Day-2_Activity1/activity_day2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<html>
<body>
<script src="owl.js"></script>
<script src="activity_day2.js"></script>
</body>
</html>
73 changes: 73 additions & 0 deletions Day-2_Activity1/activity_day2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

const {Component,mount,xml,useState,useEnv,reactive}=owl

const finalData=()=>{
const counter=useEnv();
return useState(counter.store)
}

class dataList{
count=0;
updateCount(){ this.count++; }
getCount(){ return this.count; }
}


class Second extends Component{
static template=xml`
<t t-esc="props.x.x"/><br/>
<b>Please Guess the number: </b>
<input type="text" t-on-change="check" id="guess"/>
<h2 id="result" style="color:red;"></h2>
<p>No. of Trials:</p>
<p id="trials"></p>
`;
static props=["x"]

setup(){
this.counter=finalData();
}
check(evt){
this.counter.updateCount();

let num=evt.target.value;
let x=this.props.x.x;
let result=document.getElementById("result")
if(num <x){
result.innerHTML="LOWER THAN GUESS";
}
else if(num >x){
result.innerHTML="HIGHER THAN GUESS";
}
else{
result.innerHTML="CORRECT GUESS";
result.style.color='green'
}
document.getElementById("trials").innerHTML=this.counter.getCount();

}


}

class Root extends Component{
static template=xml`<b>Computer Guess:</b>
<Second x="data"/>

`;
setup(){
let x = Math.floor((Math.random() * 10) + 1);
console.log(x)
this.data=useState({x:x})

}

static components={Second};

}
const createData=()=>{
return reactive(new dataList);
}

const env={store:createData()}
mount(Root,document.body,{dev:true,env});
Loading