Skip to content

Commit 65c5880

Browse files
committed
Add files for Assignment 1
1 parent d21e3e8 commit 65c5880

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Assignment-1
2+
1. Write a javascript function named is_integer which checks if the passed argument is an integer. You can use any mathematical operator or functions defined in the Math object.
3+
2. Using the forEach function defined for an array, find the sum of the array of numbers. [function add_all(arr) {...}]
4+
3. Write a JavaScript program to convert temperatures to and from celsius, fahrenheit. [ Use the formula : c/5 = (f-32)/9, where c = temperature in celsius and f = temperature in fahrenheit]
5+
4. Write a factorial function that returns the factorial of a given number, n. Make sure you return the calculated value and not just print it. [function factorial(n){...}]
6+
5. Write a javascript function that converts a given amount of money into coins of denominations (1, 2, 5, 10 and 25). [function convert_to_coins(amount) {...}]. You may choose to print the coin denominations used on the console. E.g. convert_to_coins(87) should print 25 25 25 10 2.
7+
8+
*This project/author of this project is not endorsed, connected, or related to Udemy or its products and courses in any way. The solutions provided are just for reference and exercises.*
9+
10+
[Click Here](External_Courses\Udemy\Course\README.md) for more details
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// mySolution.js
2+
3+
/*
4+
1. Write a javascript function named is_integer which checks if the passed argument is an integer. You can use any mathematical operator or functions defined in the Math object.
5+
*/
6+
7+
function is_integer(num) {
8+
if (num % 1 === 0) {
9+
return true;
10+
} else {
11+
return false;
12+
}
13+
}
14+
const myNum = 11
15+
// const myNum = 11.05
16+
console.log(myNum)
17+
console.log(is_integer(myNum))
18+
19+
/*
20+
2. Using the forEach function defined for an array, find the sum of the array of numbers. [function add_all(arr) {...}]
21+
*/
22+
23+
const MyArr = [1, 2, 3, 4]
24+
25+
function add_all(arr){
26+
let sum = 0;
27+
arr.forEach(function(num){
28+
sum += num;
29+
});
30+
return sum;
31+
}
32+
33+
console.log(add_all(MyArr))
34+
/*
35+
3. Write a JavaScript program to convert temperatures to and from celsius, fahrenheit. [ Use the formula : c/5 = (f-32)/9, where c = temperature in celsius and f = temperature in fahrenheit]
36+
*/
37+
38+
function convert(temperature, type) {
39+
// 0 --> to celsius
40+
// 1 --> to fahrenheit
41+
if (type=0) {
42+
return (temperature - 32) * 5/9;
43+
}
44+
else if (type=1) {
45+
return (temperature * 9/5) + 32;
46+
}
47+
}
48+
49+
const temperature = 23
50+
console.log(temperature)
51+
console.log(convert(temperature, 1))
52+
53+
/*
54+
4. Write a factorial function that returns the factorial of a given number, n. Make sure you return the calculated value and not just print it. [function factorial(n){...}]
55+
*/
56+
57+
function factorial (n) {
58+
if (n === 0) {
59+
return 1
60+
}
61+
else {
62+
return n * factorial(n-1)
63+
}
64+
65+
}
66+
67+
console.log(factorial(3))
68+
69+
/*
70+
5. Write a javascript function that converts a given amount of money into coins of denominations (1, 2, 5, 10 and 25). [function convert_to_coins(amount) {...}]. You may choose to print the coin denominations used on the console. E.g. convert_to_coins(87) should print 25 25 25 10 2.
71+
*/
72+
73+
function convert_to_coins(amount) {
74+
console.log(`Converting ${amount} to coins`);
75+
let myDenominations = [1, 2, 5, 10, 25];
76+
let coins = {
77+
1: 0,
78+
2: 0,
79+
5: 0,
80+
10: 0,
81+
25: 0
82+
};
83+
let remainingAmount = amount;
84+
myDenominations.reverse()
85+
for (let i = 0; i < myDenominations.length; i += 1) {
86+
const currentDenomination = myDenominations[i];
87+
const numCoins = Math.floor(remainingAmount / currentDenomination);
88+
89+
coins[currentDenomination] += numCoins;
90+
remainingAmount -= numCoins * currentDenomination;
91+
}
92+
// Check for remaining amount
93+
if (remainingAmount > 0) {
94+
console.log(`Amount cannot be converted exactly using given denominations`);
95+
return "Amount cannot be converted exactly using given denominations";
96+
}
97+
98+
return coins;
99+
}
100+
101+
console.log(convert_to_coins(129))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# Learn to Program in Javascript: Beginner to Pro
3+
## ![Learn to Program in Javascript: Beginner to Pro](https://img.shields.io/badge/Udemy-02b87c?style=for-the-badge&logo=udemy&logoColor=white)
4+
5+
6+
7+
### [Learn to Program in Javascript: Beginner to Pro](https://www.udemy.com/course/programming-in-javascript/)
8+
9+
**View the course at**
10+
11+
https://www.udemy.com/course/programming-in-javascript/
12+
13+
*This project/author of this project is not endorsed, connected, or related to Udemy or its products and courses in any way. The solutions provided are just for reference and exercises.*

0 commit comments

Comments
 (0)