Cara menggunakan EXCER pada JavaScript

Last update on August 19 2022 21:50:50 (UTC/GMT +8 hours)

JavaScript Math: Exercise-35 with Solution

Write a JavaScript function for the Pythagorean theorem.

Table of Contents

  • JavaScript Math: Exercise-35 with Solution
  • JavaScript: Tips of the Day
  • JavaScript Loops
  • Instead of writing:
  • You can write:
  • Different Kinds of Loops
  • The For Loop
  • Expression 1
  • Expression 2
  • Expression 3
  • For/Of and For/In Loops
  • While Loops

Table of Contents

  • JavaScript Math: Exercise-35 with Solution
  • JavaScript: Tips of the Day
  • JavaScript Loops
  • Instead of writing:
  • You can write:
  • Different Kinds of Loops
  • The For Loop
  • Expression 1
  • Expression 2
  • Expression 3
  • For/Of and For/In Loops
  • While Loops

According to Wikipedia : In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem, is a relation in Euclidean geometry among the three sides of a right triangle. It states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the "Pythagorean equation".

Test Data :
console.log(pythagorean(4, 3));
5

Sample Solution:-

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Write a JavaScript function for the Pythagorean theorem</title>
</head>
<body>

</body>
</html>

JavaScript Code:

function pythagorean(sideA, sideB){
  return Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));
}

console.log(pythagorean(4, 3));

Sample Output:

5

Pictorial Presentation:

Flowchart:

Cara menggunakan EXCER pada JavaScript

Live Demo:

See the Pen javascript-math-exercise-35 by w3resource (@w3resource) on CodePen.

Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to convert radians to degrees.
Next: Write a JavaScript function which will return values that are powers of two.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

JavaScript: Tips of the Day

for-in loop

const person = {
  name: 'Emille',
  age: 23
  ,
};

for (const item in person) {
  console.log(item);
}

With a for-in loop, we can iterate through object keys, in this case name and age. Under the hood, object keys are strings (if they're not a Symbol). On every loop, we set the value of item equal to the current key it's iterating over. First, item is equal to name, and gets logged. Then, item is equal to age, which gets logged.

Ref: https://bit.ly/323Y0P6


  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion



Loops can execute a block of code a number of times.


JavaScript Loops

Loops are handy, if you want to run the same code over and over again, each time with a different value.

Often this is the case when working with arrays:

Instead of writing:

text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
text += cars[3] + "<br>";
text += cars[4] + "<br>";
text += cars[5] + "<br>";

You can write:

for (let i = 0; i < cars.length; i++) {
  text += cars[i] + "<br>";
}

Try it Yourself »


Different Kinds of Loops

JavaScript supports different kinds of loops:

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

The For Loop

The for statement creates a loop with 3 optional expressions:

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

Example

for (let i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}

Try it Yourself »

From the example above, you can read:

Expression 1 sets a variable before the loop starts (let i = 0).

Expression 2 defines the condition for the loop to run (i must be less than 5).

Expression 3 increases a value (i++) each time the code block in the loop has been executed.



Expression 1

Normally you will use expression 1 to initialize the variable used in the loop (let i = 0).

This is not always the case, JavaScript doesn't care. Expression 1 is optional.

You can initiate many values in expression 1 (separated by comma):

Example

for (let i = 0, len = cars.length, text = ""; i < len; i++) {
  text += cars[i] + "<br>";
}

Try it Yourself »

And you can omit expression 1 (like when your values are set before the loop starts):

Example

let i = 2;
let len = cars.length;
let text = "";
for (; i < len; i++) {
  text += cars[i] + "<br>";
}

Try it Yourself »

Expression 2

Often expression 2 is used to evaluate the condition of the initial variable.

This is not always the case, JavaScript doesn't care. Expression 2 is also optional.

If expression 2 returns true, the loop will start over again, if it returns false, the loop will end.

If you omit expression 2, you must provide a break inside the loop. Otherwise the loop will never end. This will crash your browser. Read about breaks in a later chapter of this tutorial.


Expression 3

Often expression 3 increments the value of the initial variable.

This is not always the case, JavaScript doesn't care, and expression 3 is optional.

Expression 3 can do anything like negative increment (i--), positive increment (i = i + 15), or anything else.

Expression 3 can also be omitted (like when you increment your values inside the loop):

Example

let i = 0;
let len = cars.length;
let text = "";
for (; i < len; ) {
  text += cars[i] + "<br>";
  i++;
}

Try it Yourself »


Loop Scope

Using var in a loop:

Example

var i = 5;

for (var i = 0; i < 10; i++) {
  // some code
}

// Here i is 10

Try it Yourself »

Using let in a loop:

Example

let i = 5;

for (let i = 0; i < 10; i++) {
  // some code
}

// Here i is 5

Try it Yourself »

In the first example, using var, the variable declared in the loop redeclares the variable outside the loop.

In the second example, using let, the variable declared in the loop does not redeclare the variable outside the loop.

When let is used to declare the i variable in a loop, the i variable will only be visible within the loop.


For/Of and For/In Loops

The for/in loop and the for/of loop are explained in the next chapter.


While Loops

The while loop and the do/while are explained in the next chapters.