Basic Algorithm Scripting exercise 5

Hello I’m currently in the challenge “Return Largest Numbers in Arrays” from the Basic Algorithm Scripting section in the JavaScript Algorithms and Data Structures certification and I came up with this solution:

I got curious and also checked the provided solutions in the challenge guide. I noticed that my solution is different from all those and I would like to get some feedback. I’m still new to this coding stuff and I don’t really know about performace on algorithms, I know it works but I have no idea how efficient it is compared to the other answers

Please post your actual code instead of a picture.

I wouldn’t get caught up in ‘performance’. For most things, as long as you write sensible code, you don’t have to worry about writing a ‘high performance’ algorithm.

1 Like

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1 Like

My bad, here is the code:

function largestOfFour(arr) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        let largestNum = 0;
        largestNum = Math.max(...arr[i]);

    return newArr;

largestOfFour([4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]);

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Got it, thanks for the answer

Small quibble - arrays generally should be declared as const.

Why not put this all on one line?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.