TODAY COMMUNITY FOR BATCH FSR060523 12 july!

Q1- Create a JavaScript class called MathUtils with a static method sumArray() that takes an array of numbers as input and returns the sum of all the numbers.

1 Like

class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}

getArea() {
return this.width * this.height;
}
}

var rectangle = new Rectangle(5, 10);

var area = rectangle.getArea();

console.log(area);

1 Like
<!DOCTYPE html>
<html>
<head>
  <title>Rectangle Class Example</title>
  <script>
    class Rectangle {
      constructor(width, height) {
        this.width = width;
        this.height = height;
      }

      getArea() {
        return this.width * this.height;
      }
    }

    function calculateArea() {
      var width = parseFloat(document.getElementById('widthInput').value);
      var height = parseFloat(document.getElementById('heightInput').value);

      var rectangle = new Rectangle(width, height);
      var area = rectangle.getArea();

      document.getElementById('result').innerHTML = 'Area: ' + area;
    }
  </script>
</head>
<body>
  <h1>Rectangle Area Calculator</h1>
  <label for="widthInput">Width:</label>
  <input type="number" id="widthInput" placeholder="Enter width" step="0.01">
  <br>
  <label for="heightInput">Height:</label>
  <input type="number" id="heightInput" placeholder="Enter height" step="0.01">
  <br>
  <button onclick="calculateArea()">Calculate Area</button>
  <br>
  <div id="result"></div>
</body>
</html>

3 Likes
 class MathUtils {
  static sumArray(array) {
    return array.reduce((a, b) => a + b);
  }
}
1 Like

class triangle{
area(){
var height=10;
var base=20;
var result=0.5heightbase;
document.write(result)
}
}
var area1=new triangle();
area1.area();

1 Like
<!DOCTYPE html>
<html>
<head>
  <title>MathUtils - sumArray</title>
  <script>
    class MathUtils {
      static sumArray(numbers) {
        let sum = 0;
        for (let i = 0; i < numbers.length; i++) {
          sum += numbers[i];
        }
        return sum;
      }
    }

    function calculateSum() {
      const numbers = [1, 2, 3, 4, 5];
      const sum = MathUtils.sumArray(numbers);
      document.getElementById('result').innerHTML = 'Sum: ' + sum;
    }
  </script>
</head>
<body>
  <h1>MathUtils - sumArray</h1>
  <button onclick="calculateSum()">Calculate Sum</button>
  <div id="result"></div>
</body>
</html>

2 Likes

class MathUtils {
static sumArray(numbers) {
return numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}
}

var numbers = [1, 2, 3, 4, 5];
var sum = MathUtils.sumArray(numbers);

console.log(sum);

1 Like

2- Create a JavaScript class called Person with properties name and age .

Code-2

<!DOCTYPE html>
<html>
<head>
  <title>MathUtils - sumArray</title>
  <script>
    const MathUtils = {
      sumArray: function(numbers) {
        return numbers.reduce(function(sum, num) {
          return sum + num;
        }, 0);
      }
    };

    function calculateSum() {
      const numbers = [1, 2, 3, 4, 5];
      const sum = MathUtils.sumArray(numbers);
      document.getElementById('result').innerHTML = 'Sum: ' + sum;
    }
  </script>
</head>
<body>
  <h1>MathUtils - sumArray</h1>
  <button onclick="calculateSum()">Calculate Sum</button>
  <div id="result"></div>
</body>
</html>

3 Likes

class MathUtils {
static sumArray(numbers) {
if (!Array.isArray(numbers)) {
throw new Error(‘’);
}

let sum = 0;
for (let i = 0; i < numbers.length; i++) {
  if (typeof numbers[i] !== 'number') {
    throw new Error('');
  }
  sum += numbers[i];
}
return sum;

}
}
const numbers = [1, 2, 3, 4, 5];
const sum = MathUtils.sumArray(numbers);
console.log(sum);

1 Like

class person{
name(){
var x=prompt(“enter name”);
document.write(x);
}
age(){
var y=prompt(“enter age”);
document.write(y);
}

}
var detail1=new person();
detail1.name();
detail1.age();

1 Like

class MathUtils{
sumArray(){
var x = [1,2,3,4,5,6];

    var ans1 = x.map((y=>{
        return y;
    }))
    console.log(ans1);
}

}
var sum = new MathUtils();
sum.sumArray();

1 Like
<!DOCTYPE html>
<html>
<head>
  <title>Person Example</title>
</head>
<body>
  <h1>Person Example</h1>
  <div id="personInfo">
    <label for="nameInput">Name:</label>
    <input type="text" id="nameInput" />
    <label for="ageInput">Age:</label>
    <input type="number" id="ageInput" />
    <button id="createPersonBtn">Create Person</button>
  </div>
  <div id="personOutput"></div>

  <script>
    // Define the Person class
    class Person {
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
    }

    // Get the required HTML elements
    const nameInput = document.getElementById('nameInput');
    const ageInput = document.getElementById('ageInput');
    const createPersonBtn = document.getElementById('createPersonBtn');
    const personOutput = document.getElementById('personOutput');

    // Event listener for creating a person
    createPersonBtn.addEventListener('click', function() {
      const name = nameInput.value;
      const age = parseInt(ageInput.value);
      const person = new Person(name, age);
      displayPerson(person);
    });

    // Function to display person information
    function displayPerson(person) {
      const personInfo = `
        <p>Name: ${person.name}</p>
        <p>Age: ${person.age}</p>
      `;
      personOutput.innerHTML = personInfo;
    }
  </script>
</body>
</html>

2 Likes

// Create a JavaScript class called Person with properties name and age .
class Person{
name(){
document.write(“He is john”);
}
age(){
document.write(“his age is 23”);
}
}
var details = new Person();
details.name();
details.age();

1 Like