Repository navigation
#027 Buggy Code2
Fix the code in the code tab to pass this challenge (only syntax errors).
Current source and tests | Catalog | Testing and tooling
From the cloned repository root, after installing dependencies:
node "Edabit/027 Buggy Code2.js"Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.
This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.
//Fix the code in the code tab to pass this challenge (only syntax errors).
function maxNum(n1, n2) {
if (n1 < n2) {
return n2
}
else {
return n1
}
}
import * as Test from '../testHelper.js';
Test.assertEquals(maxNum(3, 7), 7)
Test.assertEquals(maxNum(-1, 0), 0)
Test.assertEquals(maxNum(1000, 400), 1000)
Test.assertEquals(maxNum(-3, -9), -3)
Test.assertEquals(maxNum(88, 90), 90)Cross-language explanation preserved from the study materials
Reference file: #038 Buggy Code 2.txt. These older notes use a separate numbering scheme and may describe C# code, external diagrams or benchmark timings. They are reference material, not measured JavaScript performance. The current implementation and assertions above are authoritative.
# [Buggy Code (Part 5)](https://edabit.com/challenge/RdEdQh4kDn8BLhBbf)
Published by [Mubashir Hassan](https://edabit.com/user/T6iBEE2jp7f7iEF2P)
bugs data_structures loops numbers
**Mubashir** created an infinite loop! Help him by fixing the code in the code tab to pass this challenge. Look at the examples below to get an idea of what the function should do.
`printArray(1) ➞ [1]`
`printArray(3) ➞ [1, 2, 3]`
`printArray(6) ➞ [1, 2, 3, 4, 5, 6]`
**READ EVERY WORD CAREFULLY, CHARACTER BY CHARACTER!**
Don't overthink this challenge; it's not supposed to be hard.
### Time Complexity:
The time complexity of this function is _O(n)_, where 𝑛 is the input value `number`. This is because we iterate from 1 to `number`, adding each integer to the list.
### Space Complexity:
The time complexity of this function is _O(n)_, where 𝑛 is the input value `number`. This is because we iterate from 1 to `number`, adding each integer to the list.

//Look at the examples below to get an idea of what the function should do.
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
namespace Challenges
{
public class Loop
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0090:Use 'new(...)'", Justification = "Intentional")]
public static List<int> PrintArray(int number)
{
List<int> array = new List<int>();
for (int counter = 1; counter <= number; counter++)
{
array.Add(counter);
}
return array;
}
}
public class BenchmarkProgram38
{
[Benchmark]
[Arguments(1)]
[Arguments(2)]
[Arguments(6)]
public List<int> PrintArray(int number) => Loop.PrintArray(number);
}
}
using System;
using NUnit.Framework;
using System.Collections.Generic;
using Challenges.Edabit;
namespace Tests
{
[TestFixture]
public class Tests38
{
[TestCase(1, new int[] { 1 })]
[TestCase(2, new int[] { 1, 2 })]
[TestCase(6, new int[] { 1, 2, 3, 4, 5, 6 })]
public static void TestPrintArray(int n, int[] expectedValues)
{
List<int> myValues = new(expectedValues);
Assert.That(Loop.PrintArray(n), Is.EqualTo(myValues));
}
}
}