Today we solve 10 basic problems using javascript, it’s very easy to learn, also a trending language today.
1. To Add Two Numbers
We’re currently practicing performing basic addition operations with two integer numbers.
function sum(a, b) {
return a + b;
}
var x = sum(5,6)
console.log(x);
# input 5 , 6
# output 11
2. Convert Celsius To Fahrenheit
We can implement the formula for converting Celsius to Fahrenheit in Python code by using the expression (0°C × 9/5) + 32 = 32°F.
function Celsius(c_temp) {
return ((c_temp*9)/5)+32;
}
var f_temp = "Fahrenheit: "+Celsius(36);
console.log(f_temp);
# input 36
# output 96.8
3. Convert Kilometers To Miles in javascript
We know 1 kilometer is approximately equal to 0.62 miles, we can easily calculate the distance in miles by multiplying the distance in kilometers by 0.62.
function kmToMiles(km) {
return km*0.62;
}
var km = "Miles: "+kmToMiles(5);
console.log(km);
# input 5
# output 3.1
4. Is A Number Positive Or Negative
We can easily determine whether a number is positive or negative using this logic: if the number is equal to or greater than zero, then it’s positive; otherwise, it’s negative.
function isPositive(num) {
if (num >=0) {
var check = "positive";
}
else {
var check = "negetive";
}
return check;
}
num = 2
console.log(isPositive(num));
# input 2
# output positive
# input -3
# output negetive
5. Is A Number Even Or Odd
We can determine whether a number is even or odd by dividing it by 2 and checking the remainder (modulus). If the remainder is zero, then the number is even; otherwise, it’s odd.
function evenOrOdd(num) {
if (num !=0) {
if (num%2==0) {
check = "even"
}
else {
var check = "odd";
}
return check
}
else {
"not even or odd"
}
}
console.log(evenOrOdd(6))
# input 2
# output even
# input 3
# output odd
6. Find the Largest Number from a List
Here, we’re leveraging Python’s default function max()
. Later on, we’ll explore implementing custom methods using loops or recursion.
function maxnum(num) {
var temp_v = num[0];
for (var i = 0; i < num.length; i++) {
if (temp_v<num[i]) {
temp_v=num[i];
}
}
return temp_v;
}
var num = [5,10,2,9];
console.log(maxnum(num))
# input [2,5,3,1,4]
# output 5
7. Determine whether a year is a leap year or not
We can determine whether a year is a leap year by the following rules: if the year is divisible by 400, it’s a leap year. If it’s not divisible by 400 but divisible by 4 and not divisible by 100, then it’s also a leap year; otherwise, it’s not leap year.
function isLeapYear(year) {
if ( year%400==0 || (year%4==0 && year%100!=0)) {
console.log("Leap Year")
}
else{
console.log("Not Leap Year")
}
}
isLeapYear(1996)
# input 1996
# output leap year
# input 2300
# output not leap year
8. Find The Factorial Of A Number
The factorial of a non-negative integer 𝑛n, denoted by 𝑛!n!, is the product of all positive integers less than or equal to 𝑛n. For example:
𝑛!=𝑛×(𝑛−1)×(𝑛−2)×…×3×2×1n!=n×(n−1)×(n−2)×…×3×2×1
For instance, 5!=5×4×3×2×1=1205!=5×4×3×2×1=120.
function factorial(num) {
var output=1;
while (num>1) {
output *= num;
num = num -1;
}
return output
}
console.log(factorial(5))
# input 5
# output 120
see same problems solutions in python
If you don’t understand the syntax properly, then learn from here