Skip to content

Day 1 assignment. #12

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

Open
wants to merge 2 commits into
base: master
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
77 changes: 77 additions & 0 deletions Day-1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="script.js"></script>
<title>My App</title>
</head>
<body>
<div>
<h3>Factorial</h3>
<p>Please enter number</p>
<input type="text" id="factorial" />
<button onclick="factorial()"> Get Factorial</button>
<p>Output:</p>
<p id="getfactorial"></p>
</div>
<div>
<h3>Sum of Natural Numbers</h3>
<p>Please click</p>
<button onclick="natural()"> Get Sum</button>
<p>Output:</p>
<p id="natural"></p>
</div>
<div>
<h3>Power of 2</h3>
<p>Please enter number</p>
<input type="text" id="power" />
<button onclick="powerCheck()"> Get Value</button>
<p>Output:</p>
<p id="getpower"></p>
</div>
<div>
<h3>Fizz Buzz</h3>
<p>Please enter number</p>
<input type="text" id="multiples" />
<button onclick="multiplesCheck()"> Get Value</button>
<p>Output:</p>
<p id="getmultiple"></p>
</div>
<div>
<h3>Array Function</h3>
<p>Find all pairs in [1, 4, 3, 5, 4, 6, 7, 8, 3] of integers whose sum is equal to 8.</p>
<button onclick="arrayFunc()"> Get Value</button>
<p>Output:</p>
<p id="finalArray"></p>
</div>
<div>
<h3>Array Repeat</h3>
<p>Print all the numbers that are repeating in [1, 2, 3, 5, 8, 4, 7, 9, 1, 4, 12, 5, 6, 5, 2, 1, 0, 8, 1]</p>
<button onclick="arrayRepeat()"> Get Value</button>
<p>Output:</p>
<p id="finalArrayRepeat"></p>
</div>
<div>
<h3>Object</h3>
<p>Get amount paid in {"Rick": 85, "Amit": 42, "George": 53, "Tanya": 60, "Linda": 35}</p>
<button onclick="amountPaid()"> Get Amount</button>
<p>Output:</p>
<p id="amount"></p>
</div>
<div>
<h3>Palindrome</h3>
<p>Please enter a string</p>
<input type="text" id="palindrome" />
<button onclick="palindromeCheck()">Check if it is palindrome</button>
<p>Output:</p>
<p id="getpalindrome"></p>
</div>
<div>
<h3>Palindrome in array</h3>
<p>Please return palidrome in ["Malayalam", "tree", "boat", "civic", "melt", "level"]</p>
<input type="text" id="palindromeArray" />
<button onclick="palindromeArray()">Get palindromes</button>
<p>Output:</p>
<p id="getpalindromeArray"></p>
</div>
</body>
</html>
112 changes: 112 additions & 0 deletions Day-1/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
var factorial = function() {
var value = document.getElementById("factorial").value;
var factorial = 1;
for (var idx = 1; idx <= value; idx++) {
factorial = factorial * idx;
}
document.getElementById("getfactorial").innerHTML = factorial;
};
var natural = function() {
var natural = 0;
for (var idx = 1; idx <= 500; idx++) {
natural = natural + idx;
}
document.getElementById("natural").innerHTML = natural;
};
var naturalMultiples = function() {
var naturalMultiple = 0;
for (var idx = 1; idx < 1000; idx++) {
if (idx % 3 === 0 || idx % 5 === 0) {
naturalMultiple = naturalMultiple + idx;
}
}
document.getElementById("naturalMultiple").innerHTML = naturalMultiple;
};
var powerCheck = function() {
var value = document.getElementById("power").value;
var power;
for (var idx = 1; idx <= value; idx++) {
if (Math.pow(2, idx) <= value) {
power = idx;
} else {
break;
}
}
document.getElementById("getpower").innerHTML = power;
};
var multiplesCheck = function() {
var value = document.getElementById("multiples").value;
var multiples = [];
for (var idx = 1; idx <= value; idx++) {
if (idx % 3 === 0) {
multiples.push("Fizz");
} else if (idx % 5 === 0) {
multiples.push("Buzz");
} else {
multiples.push(idx);
}
}
document.getElementById("getmultiple").innerHTML = multiples.join(" , ");
};
var arrayFunc = function() {
var array = [1, 4, 3, 5, 4, 6, 7, 8, 3];
var sum = 8;
var finalArray = [];
for (var i = 0; i <= array.length; i++) {
for (var idx = i + 1; idx <= array.length; idx++) {
if (array[i] + array[idx] === sum) {
finalArray.push(array[i] + " , " + array[idx]);
}
}
}
document.getElementById("finalArray").innerHTML = finalArray.join("\r\n");
};
var arrayRepeat = function() {
var array = [1, 2, 3, 5, 8, 4, 7, 9, 1, 4, 12, 5, 6, 5, 2, 1, 0, 8, 1, 12, 9];
var finalArray = [];
for (var i = 0; i <= array.length; i++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to see if you avoid 2 levels of looping ?

for (var idx = i + 1; idx <= array.length; idx++) {
if (array[i] === array[idx] && finalArray.indexOf(array[i]) < 0) {
finalArray.push(array[i]);
}
}
}
document.getElementById("finalArrayRepeat").innerHTML = finalArray.join(" , ");
};
var amountPaid = function() {
var amount = {
"Rick": 85,
"Amit": 42,
"George": 53,
"Tanya": 60,
"Linda": 35
};
var amountPaid = 0;
for (var idx in amount) {
amountPaid = amountPaid + amount[idx];
}
document.getElementById("amount").innerHTML = amountPaid;
};
var palindromeCheck = function(palindromeValue) {
var value = (palindromeValue) ? palindromeValue : document.getElementById("palindrome").value;
var palindrome = value.split("");
if (value === palindrome.reverse().join("")) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to see if you avoid using reverse() and join() to achieve this ?

(!palindromeValue) ? document.getElementById("getpalindrome").innerHTML = "It is a palindrome": "";
return true;
} else {
(!palindromeValue) ? document.getElementById("getpalindrome").innerHTML = "It is not a palindrome": "";
return false;
}

};
var palindromeArray = function() {
var finalArray = [];
var array = ["Malayalam", "tree", "boat", "civic", "melt", "level"];
for (var idx = 0; idx < array.length; idx++) {
var a = array[idx].toLowerCase();
if (palindromeCheck(a)) {
finalArray.push(array[idx]);
}
}
document.getElementById("getpalindromeArray").innerHTML = finalArray.join(" , ");
};