- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
Latest commit
162 lines (138 loc) · 4.03 KB
/
Copy pathsolution.js
File metadata and controls
162 lines (138 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//1.store name and display it in alert
//let MyName = "Fisher Joseph";
//alert(MyName);
//2.declare two number variable
letnumber1=8;
letnumber2=4;
console.log("sum =",number1+number2);
console.log("Difference =",number1-number2);
console.log("product =",number1*number2);
console.log("quotient =",number1/number2);
//3.convert celsius to farenheit
functioncelsiusTofarenheit(celsius){
return(celsius*9/5)+32;
}
letinput=prompt("Enter temperature in °C: ");
letcelsius=parseFloat(input);
if(!isNaN(celsius)){
letfarenheit=celsiusTofarenheit(celsius);
console.log("$ {celsius}°C is equal to ${farenheit.to.Fixed(2)}°F");
}
else{
console.log("Enter valid number");
}
//4.area of rectsangle
functioncalculateArea(length,width){
returnlength*width;
}
letlength=prompt("Enter the length of the rectangle: ");
letwidth=prompt("Enter the width of the rectangle: ");
length=parseFloat(length);
width=parseFloat(width);
if(!isNaN(length)&&!isNaN(width)){
letarea=calculateArea(length,width);
console,log("The area of the rectangle is: ${area} square units.");
}
else{
console.log("Enter valid numbers")
}
//5.code on od and even number
letinput0=prompt("Enter a number:");
letnumber=parseInt(input0);
if(isNaN(number)){
console.log("${number} is Even.");
}
else{
console.log("Enter valid number.");
}
//6. program for checking leap year
functionisLeapYear(year){
return(year%4===0&&year%100!==0)||(year%400===0);
}
constinputYear=parseInt(prompt("Enter year: "));
alert(
inputYear+(isLeapYear(inputYear) ? "is it a leap year." : "is not a leap year.")
);
//7. revesing a string input
functionreverseString(str){
returnstr.split("").reverse().join("");
}
constinput1=prompt("Enter a string: ");
alert("Reversed string: "+reverseString(input1));
//8.counting number of vowels in string
functioncountvowels(str){
constvowels="aeiouAEIOU";
letcount=0;
for(leti=0;i<str.length;i++){
if(vowels.includes(str[i])){
count++;
}
}
returncount;
}
constinput2=prompt("Enteer a string");
alert("Number of vowels: "+countvowels(input2));
//9.finding largest number in an array
functionfindLargestNumber(arr){
letmax=arr[0];
for(leti=1;i<arr.length;1++){
if(arr[1]>max){
max=arr[i];
}
}
returnmax;
}
constnumbers=[];
for(leti=0;i<5;i++){
letnum=parseFloat(prompt("Enter number ${i + 1}:"));
numbers.push(num);
}
alert("The largest number is: "+findLargestNumber(numbers));
//10. checking if a string is a palindrome
functionIsPalindrome(str){
constcleaned=str.toLowerrCase().replce(/[^a-z0-9]/g,"");
cleaned.split("").reverse().join("");
returncleaned===reserved;
}
//11. calculating factorial of a number
functionfactorial(n){
if(n<0)returnundefined;
if(n===0||n===1)return1;
returnn*factorial(n-1);
}
//12. generating random numbers btw two given values
functiongetRandomInt(mini,maxi){
mini=Math.cell(mini);
maxi=Math.floor(maxi);
returnMath.floor(Math.random()*(maxi-mini+1))+mini;
}
//13. converting seconds to hours, mins, and secs
functionconvertSeconds(totalsecs){
consthours=Math.floor(totalsecs/3600);
constminutes=Math.floor(totalsecs%3600)/60;
constseconds=totalsecs%60;
return{
hours: hours,
minutes: minutes,
seconds: seconds
};
}
//14. checking if a number is a prime#
functionisPrime(n){
if(n<=1)returnfalse;
if(n===2)returntrue;
if(n%2===0)returnfalse;
constsqrt=Math.sqrt(n);
for(leti=3;i<=sqrt;i+=2){
if(n%i===0)returnfalse;
}
returntrue;
}
//15. function that capitalizes the first letter of each word in a sentence
functioncapitalizeWords(sentence){
returnsentence
.toLowerCase()
.split(" ")
.map(word=>word.charAt(0).toUpperCase()+word.slice(1))
.join("");
}