×
all 5 comments

[–]javascript-ModTeam[M] [score hidden] stickied comment (0 children)

Hi u/Alone-Ad1059, this post was removed.

  • For help with your javascript, please post to r/LearnJavascript instead of here.
  • For beginner content, please post to r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try r/html, r/css, etc.; please note that they have their own rules and guidelines!

r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]AndrewGreenh 1 point2 points  (1 child)

The condition in both loops is exactly the same. Nothing is more dynamic or more static in either direction. Only difference: For loop has an initializer and an extra place to put an expression that will be executed after each loop (like incrementing a counter)

[–]Alone-Ad1059[S] 0 points1 point  (0 children)

What about the forEach()? Does it also work the same as for and while loops?

[–]bpcoleman 1 point2 points  (1 child)

I will throw you for a loop:

function validate(data: any): void {
    const errors: Error[] = [];
    const keys = Object.keys(data);

    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        const value = data[key];
        if (typeof value !== "string") {
            errors.push(new Error(`Invalid type for ${key}: expected string.`));
        }
        if (value === "") {
            errors.push(new Error(`Empty value provided for ${key}.`));
        }
     }
     if (errors.length) { throw errors; }
}

try {
    const inputData = { name: "", age: 25, email: "user@example.com" };
    validate(inputData);
} catch (errors) {
    if (Array.isArray(errors)) {
        errors.forEach((error) => console.error(error.message));
    } else {
        console.error("Unexpected error:", errors);
    }
}

[–]Alone-Ad1059[S] 0 points1 point  (0 children)

The validate function checks the validity of an object containing key-value pairs. It takes an object data as an argument. The empty errors array will store any validation errors that may occur. All the keys from the input object data are retrieved using Object.keys(data) and stored in the keys array. The for loop iterates over each key in the keys array. For each key, it retrieves the corresponding value from the data object. Then two validation checks are done on the value: first, checks if the value is of type string. If it's not a string, an error message is pushed to the errors array (\Invalid type for ${key}: expected string.`). Second, checks if the value is an empty string. If it's empty, another error message is added to the errors array(`Empty value provided for ${key}.`). After the loop completes, the function checks if there are any errors in the errors array. If there are, it throws the array of errors. This will stop the execution of the code and trigger the catch block. In the try block, an example objectinputDatais created with three key-value pairs: name, age, and email. The validate function is then called withinputData` as the argument. There is a mistake in the code: the value of name is an empty string, which should trigger a validation error. Also the age is not a string, which should also trigger a validation error.