Hướng dẫn if else statement array javascript - if else mảng câu lệnh javascript

Tâm trí của tôi đã đưa ra giải pháp này:

function not(dat, arr) { //"not" function
for(var i=0;i

0 điểm

Hướng dẫn if else statement array javascript - if else mảng câu lệnh javascript

hơn 9 năm

Vì vậy, tôi đang cố gắng viết một mã in ra tất cả các tên trong mảng với một câu mô tả độ dài ký tự của mỗi tên. Tôi biết rằng vấn đề này nằm trong câu lệnh IF, nhưng tôi không chắc định dạng sẽ là gì. Nó sẽ là String.length.Names?

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}

Trả lời 51605AAA666C21274D30003F4

Patrick, bạn phải suy nghĩ về những gì bạn muốn làm việc. Hãy suy nghĩ nếu bạn muốn tham khảo toàn bộ mảng (tên) hoặc một phần tử trong mảng (tên [i]).

  1. Trong vòng lặp FOR, bạn gọi mảng của bạn là mảng thay vì theo tên của nó để đó là một vấn đề - dễ dàng sửa.
  2. String.Names.length là phần mà bạn thực sự hỏi về. Bạn muốn làm việc với các tên phần tử [i].
  3. Khi bạn đăng nhập, bạn cũng muốn tham khảo tên [i] không phải toàn bộ mảng như bạn đang làm ở đó.

điểm

Hướng dẫn if else statement array javascript - if else mảng câu lệnh javascript

Gửi bởi Judy

hơn 9 năm

Tuyên bố

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3 thực hiện một tuyên bố nếu một điều kiện được chỉ định là sự thật. Nếu điều kiện là giả, một tuyên bố khác trong mệnh đề
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
4 tùy chọn sẽ được thực thi.
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3
statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
4 clause will be executed.

Thử nó

Cú pháp

if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
5

Một biểu hiện được coi là sự thật hoặc giả.

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
6

Tuyên bố được thực hiện nếu điều kiện là sự thật. Có thể là bất kỳ tuyên bố nào, bao gồm cả các tuyên bố

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3 thêm. Để thực thi nhiều câu lệnh, hãy sử dụng câu lệnh BLOCK (
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
8) để nhóm các câu lệnh đó. Để thực hiện không có câu lệnh, sử dụng một câu lệnh trống.

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
9

Tuyên bố được thực thi nếu

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
5 là giả mạo và mệnh đề
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
4 tồn tại. Có thể là bất kỳ tuyên bố nào, bao gồm các câu lệnh khối và các câu lệnh
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3 thêm.

Sự mô tả

Nhiều câu lệnh

if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
3 có thể được lồng để tạo ra một mệnh đề
if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
4. Lưu ý rằng không có từ khóa
if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
5 (trong một từ) trong JavaScript.

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN

Để xem cách hoạt động của nó, đây là cách nó sẽ trông như thế nào nếu việc làm tổ được thụt vào đúng cách:

if (condition1)
  statement1
else
  if (condition2)
    statement2
  else
    if (condition3)
      statement3
// …

Để thực thi nhiều câu lệnh trong một mệnh đề, hãy sử dụng câu lệnh khối (

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
8) để nhóm các câu lệnh đó.

if (condition) {
  statements1
} else {
  statements2
}

Không sử dụng các khối có thể dẫn đến hành vi khó hiểu, đặc biệt là nếu mã được định dạng bằng tay. Ví dụ:

function checkValue(a, b) {
  if (a === 1)
    if (b === 2)
      console.log("a is 1 and b is 2");
  else
    console.log("a is not 1");
}

Mã này trông vô tội - tuy nhiên, thực thi

if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
7 sẽ ghi nhật ký "A không phải là 1". Điều này là do trong trường hợp treo lủng lẳng khác, mệnh đề
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
4 sẽ được kết nối với mệnh đề
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3 gần nhất. Do đó, mã ở trên, với thụt lề thích hợp, sẽ trông giống như:

function checkValue(a, b) {
  if (a === 1)
    if (b === 2)
      console.log("a is 1 and b is 2");
    else
      console.log("a is not 1");
}

Nói chung, đó là một thông lệ tốt để luôn sử dụng các câu lệnh khối, đặc biệt là trong mã liên quan đến các câu lệnh

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3 lồng nhau.

function checkValue(a, b) {
  if (a === 1) {
    if (b === 2) {
      console.log("a is 1 and b is 2");
    } else {
      console.log("a is not 1");
    }
  }
}

Đừng nhầm lẫn các giá trị boolean nguyên thủy

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
1 và
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
2 với sự thật hoặc giả của đối tượng
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
3. Bất kỳ giá trị nào không phải là
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
2,
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
5,
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
6,
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
7,
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
8,
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
9 hoặc chuỗi trống (
if (condition1)
  statement1
else
  if (condition2)
    statement2
  else
    if (condition3)
      statement3
// …
0) và bất kỳ đối tượng nào, bao gồm đối tượng boolean có giá trị là
if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN
2, được coi là sự thật khi được sử dụng làm điều kiện. Ví dụ:

const b = new Boolean(false);
if (b) // this condition is truthy
  statement

Ví dụ

Sử dụng nếu ... khác

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
0

Sử dụng khác nếu

Lưu ý rằng không có cú pháp

if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
5 trong JavaScript. Tuy nhiên, bạn có thể viết nó với một khoảng trống giữa
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
4 và
var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
3:

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
1

Sử dụng một bài tập làm điều kiện

Bạn hầu như không bao giờ nên có một

if (condition)
  statement1

// With an else clause
if (condition)
  statement1
else
  statement2
3 với một nhiệm vụ như
if (condition1)
  statement1
else
  if (condition2)
    statement2
  else
    if (condition3)
      statement3
// …
6 như một điều kiện:

var names = ["Jeff", "Bob", "Joe", "John", "Terri", "Sara", "Laura", "Josh", "David", "Tim"];

for (i = 0; i < array.length; i++)
{
    if(string.names.length > 3)
    {
        console.log("The name " + names + " has more than three letters!");
    }
    else
    {
        console.log("The name " + names + " has equal to or less than three letters!");
    }
}
2

Tuy nhiên, trong trường hợp hiếm hoi bạn thấy mình muốn làm điều gì đó như thế, tài liệu

if (condition1)
  statement1
else
  if (condition2)
    statement2
  else
    if (condition3)
      statement3
// …
7 có một bài tập làm phần điều kiện với một ví dụ cho thấy cú pháp thực hành tốt nhất mà bạn nên biết và làm theo.

Thông số kỹ thuật

Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # Sec-IF-Statement
# sec-if-statement

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

Xem thêm

Chúng ta có thể sử dụng nếu

Bạn không áp dụng if-else trong một mảng..

Làm thế nào để bạn viết một mảng trong điều kiện nếu điều kiện?

Ví dụ: nếu điều kiện «a» là một mảng booleans (giá trị đúng hoặc sai), nó sẽ trả về một mảng có cùng chỉ số, chứa «b» hoặc «c» khi thích hợp: biến x: = -2 ..2.Nếu x> 0 thì 'dương' khác nếu x <0 thì 'âm' khác 'không' →Variable X := -2 .. 2. If X > 0 THEN 'Positive' ELSE IF X < 0 THEN 'Negative' ELSE 'Zero' →

Nếu là gì

Câu lệnh IF/ELSE thực thi một khối mã nếu một điều kiện được chỉ định là đúng.Nếu điều kiện là sai, một khối mã khác có thể được thực thi.Câu lệnh IF/ELSE là một phần của các câu lệnh "có điều kiện" của JavaScript, được sử dụng để thực hiện các hành động khác nhau dựa trên các điều kiện khác nhau.executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. The if/else statement is a part of JavaScript's "Conditional" Statements, which are used to perform different actions based on different conditions.

Có một Elif trong JavaScript?

Trong JavaScript, chúng tôi có các câu lệnh có điều kiện sau: Sử dụng nếu chỉ định một khối mã sẽ được thực thi, nếu một điều kiện được chỉ định là đúng.Sử dụng khác để chỉ định một khối mã sẽ được thực thi, nếu cùng một điều kiện là sai.Sử dụng khác nếu chỉ định một điều kiện mới để kiểm tra, nếu điều kiện đầu tiên là sai.Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.