-
Notifications
You must be signed in to change notification settings - Fork 15
[feat] Ngan Le practices JS #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
1686ce5 to
fb79986
Compare
|
| GitGuardian id | Secret | Commit | Filename | |
|---|---|---|---|---|
| - | OpenWeatherMap Token | fb79986 | programming-language-for-tester-assignment/NganLe/nodejs-project/index.js | View secret |
🛠 Guidelines to remediate hardcoded secrets
- Understand the implications of revoking this secret by investigating where it is used in your code.
- Replace and store your secret safely. Learn here the best practices.
- Revoke and rotate this secret.
- If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.
To avoid such incidents in the future consider
- following these best practices for managing and storing secrets including API keys and other credentials
- install secret detection on pre-commit to catch secret before it leaves your machine and ease remediation.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
Our GitHub checks need improvements? Share your feedbacks!
| - Explanation: Teaches array manipulation methods. | ||
| */ | ||
| function reverseOrder(array) { | ||
| return array.reverse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Em nên kiểm tra điều kiện là parameter array có phải là kiểu mảng không trước khi sử dụng hàm reverse(), thực tế khi viết ra 1 function thì phải xem xét việc function đó sẽ được người khác sử dụng và mình cũng ko chắc chắn được liệu người ta có input đúng data type mình yêu cầu không.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nếu không sử dụng ham reverse() thì em sẽ thực hiện giải pháp này như thế nào (chỉ dùng thuần túy for loop)
| */ | ||
| function returnNumberGreaterThan10(arr) { | ||
| return arr.filter(function (value) { | ||
| return value > 10; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Em có thể coi thêm Arrow function expressions. Ở dưới đã thấy em có sử dụng
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chỗ này cũng chưa validate là array này có phải là 1 array chưa toàn bộ là number hay không
| - Use a switch statement to log a greeting based on the current day of the week. | ||
| - Explanation: Teaches the switch case control structure. | ||
| */ | ||
| let today = new Date().getDay(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chỗ này nên dùng const vì today sẽ là biến không cần khởi tạo hoặc gán lại giá trị mới khi chạy
| */ | ||
| class Car { | ||
| constructor(model, year) { | ||
| this.model = model; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Em có thể mở rộng thêm chỗ này về keyword this trong JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
| console.error(error); | ||
| } | ||
| } | ||
| useAsyncAwait() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useAsyncAwait() chưa có keyword await, đối với keyword await thì nó luôn được sử dụng bên trong 1 function vì thế chỗ này sẽ hơi tricky trong việc implement để mô phỏng keyword await
async function useAsyncAwait() {
try {
const message = await new Promise(function (resolve, reject) {
setTimeout(() => {
resolve('Success!');
}, 2000);
})
console.log(message);
} catch (error) {
console.error(error);
}
}
useAsyncAwait();
// Lúc này thì console sẽ in ra "a", trong khi mình muốn là phải in ra success trước;
console.log("a");
async function useAsyncAwait() {
try {
const message = await new Promise(function (resolve, reject) {
setTimeout(() => {
resolve('Success!');
}, 2000);
})
console.log(message);
} catch (error) {
console.error(error);
}
}
// Mình sẽ tạm wrap lại bằng 1 immediately invoked function
(async () => {
await useAsyncAwait()
console.log("a");
})();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chỗ này em có khởi tạo await ở
const message = await new Promise
Theo ý của anh thì em sẽ viết lại là
function useAsyncAwait() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve('Success!');
}, 2000);
})
}
(async () => {
await useAsyncAwait()
console.log("a");
})();
| function countChar(str, char) { | ||
| let count = 0; | ||
| for (let i = 0; i < str.length; i++) { | ||
| if (str[i] == char) count++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
str[i] == char sẽ sai khi so sánh trường hợp input là số console.log(countChar('hello1hello1', 1));
| function replaceDivisible(number) { | ||
| let outputStr = ''; | ||
| for (let i = 1; i <= number; i++) { | ||
| if (i % 3 === 0 && i % 5 !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nên khởi tạo 1 biến kiểu bool để đại diện cho "i % 3 === 0 && i % 5 !== 0" ví dụ
const isPrintFizz = i % 3 === 0 && i % 5 !== 0;
if (isPrintFizz) outputStr += 'Fizz';
Khi đó code sẽ dễ đọc và dễ hiểu hơn
| }) | ||
| } | ||
| function createActiveProfileFile() { | ||
| const fs = require('fs'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Những lệnh liên quan require/import thì luôn đưa lên đầu file. Import là syntax của JavaScript ES6
| } | ||
| // ***Function to fetch weather data using the request library | ||
| function fetchWeatherWithRequest(lat, lon, callback) { | ||
| const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URL này ở 3 function đều như nhau thì nên đưa ra bên ngoài và khai báo chung biến const url;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chỗ này url nhưng lại chứa tham số lat, lon được truyền vào khi gọi function thì làm sao để đưa ra bên ngoài để khai báo chung được ạ?
| - Explanation: Use closures or the new `#` syntax for private fields. | ||
| */ | ||
| class Encapsulation { | ||
| #privateProperties; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thực tế thì ở JavaScript và Python hoặc những ngôn ngữ dạng interpreted language thì khái niệm private không thật sự private, nó chỉ là quy định về syntax để mọi người cùng hiểu đó là thuộc tính có tính private
No description provided.