Practice set on Loops and Functions in JavaScript

//Practice set of Loops and Functions
//Question 1 : Write a program to print the marks of a student in an object using for loop
let marks = {
    harry: 90,
    shubham: 9,
    lovish: 56,
    Monika: 4
}
for (let i = 0; i < Object.keys(marks).length; i++) {
    console.log("The marks of " + Object.keys(marks)[i] + " are " + marks[Object.keys(marks)[i]]);
}

//Question 2 : Write a program in Question 1 using for in loop
for (let key in marks) {
    console.log("The marks of " + key + " are " + marks[key]);
}

//Question 3 : Write a program to print "try again" until the user enters the correct number
let cn = 43;
let i;
while (i != cn) {
    console.log("Try again");
    i = prompt("Enter a number : ");
}
console.log("You have entered a correct number");

//Question 4 : Write a function to find mean of 5 numbers
let p = prompt("Enter the value of a :");
p = Number.parseInt(p);
let q = prompt("Enter the value of b : ");
q = Number.parseInt(q);
let r = prompt("Enter the value of c : ");
r = Number.parseInt(r);
let s = prompt("Enter the value of d : ");
s = Number.parseInt(s);
const mean = (a, b, c, d) => {
    return (a + b + c + d) / 4;
}

console.log(mean(p, q, r, s));

Comments

Popular posts from this blog

Symbols tutorial in Advance JavaScript

Async and Await Method tutorial in Advance JavaScript

String Methods in JavaScript