LẬP TRÌNH CĂN BẢN - Trang 75

/codegym.vn/ - 70

Bước 5: Nếu userName là “Admin”. Khai báo biến pass nhận giá trị nhập vào từ hộp
thoại.

1.

let

pass

=

prompt

(

'Password?'

,

''

);


Bước 6: Kiểm thử

1.

if

(

pass

==

'TheMaster'

)

{

2. alert

(

'Welcome!'

);

3.

}

else

if

(

pass

==

null

)

{

4. alert

(

'Canceled.'

);

5.

}

else

{

6. alert

(

'Wrong password'

);

7.

}


Bước 7: Thực thi chương trình. Quan sát kết quả.
Mã nguồn tham khảo:

https://github.com/codegym-vn/introduction-to-programming-

with-javascript/tree/dev/chapter-03/03-luyen-tap-voi-cau-truc-if-else-if

Bài 4: Luyện tập với cấu trúc switch...case

Mục tiêu:
Luyện tập với cấu trúc switch...case
Mô tả:
Viết lại cấu trúc if sau thành cấu trúc switch...case

1.

if

(

browser

==

'Edge'

)

{

2. alert

(

"You've got the Edge!"

);

3.

}

else

if

(

browser

==

'Chrome'

||

browser

==

'Firefox'

||

browser

==

'Safari'

||

browser

==

'Opera'

)

{

4. alert

(

'Okay we support these browsers too'

);

5.

}

else

{

6. alert

(

'We hope that this page looks ok!'

);

7.

}


Hướng dẫn
Cấu trúc if được viết lại với cấu trúc switch...case như sau:

1.

switch

(

browser

)

{

2.

case

'Edge'

:

3. alert

(

"You've got the Edge!"

);

4.

break

;

5.

case

'Chrome'

:

6.

case

'Firefox'

:

7.

case

'Safari'

:

8.

case

'Opera'

:

9. alert

(

'Okay we support these browsers too'

);

10.

break

;

11.

default

:

12. alert

(

'We hope that this page looks ok!'

);

13.

}