Javascript - Thêm chữ "Py" vào đầu chuỗi
Thêm chữ "Py" vào đầu chuỗi
Viết chương trình JavaScript để tạo một chuỗi mới thêm "Py" vào trước một chuỗi đã cho. Nếu chuỗi đã cho bắt đầu bằng "Py" thì trả về chuỗi ban đầu.
Mô tả:
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to create a new string adding “Py” in front of a given string. If the given string begins with “Py” then return the original string.</title>
<script>
function string_check(str1) {
if (str1 === null || str1 === undefined || str1.substring(0, 2) === 'Py')
{
return str1;
}
return "Py"+str1;
}
document.write(string_check("Python"));
document.write(string_check("thon"));
</script>
</head>
<body>
</body>
</html>
Lưu đồ thuật toán: