Online Editor test

Tell us what’s happening:
Describe your issue in detail here.

This is more a suggestion than a question while trying to solve challenges, I want to be able to test my solutions step by step. But the platform just take a complet working answer of the challenge. I’m wondering if it is pedagogical to see result of my code small iteration before solving the whole problem asked ?
Your code so far


function sumAll(arr) {
let sum = 0;
  for (let i = 0 ; i <= arr.length-1 ; i++){
    return sum += arr[i];
  }
}

sumAll([1, 4]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36

Challenge: Sum All Numbers in a Range

Link to the challenge:

1 Like

For more complicated things JS things (like some of the algorithms) I often worked them in codepen or locally in the command line.

2 Likes

you can use as many console.log as you need to see what values are at certain lines

1 Like

Yes, it can be helpful.

1 Like

By this time you should have completed the Debugging section of the JS curriculum, where various practices to check and test your code(even by segments) are present and explained. The challenges themselves, often output test of several parameters before the final solution is being accepted. The challenges themselves can actually be looked as smaller problems which can eventually be present in a large application, a project, which can consist of tens and thousands of for loops, functions and data segments, so its really important you learn to write your code(and test it) without being monitored on every step. You should also be aware, very often, more than one solution is available for a challenge and the test suite cant track and correct each one of them; its often meant to encourage varous solutions and freedom for the learner.

2 Likes

There is this as well, which shows your code finning step by step. Only really useful for single functions (there is a hard limit on the amount of steps, so anything bigger and it’s generally going to refuse to run it), but it’s very useful for (eg) the algorithm challenges:

https://pythontutor.com/javascript.html#mode=edit

2 Likes

You are right about the debugging completed part I noticed it with the answer of @ilenia. I realized that one of it’s utility comes in here as answer to my question.
Thank you for your highlights about my question, I have a larger view of the course now.

1 Like

Thank you for your suggestion.

1 Like

for debugging I would do something like

function sumAll(arr) {
console.log({arr})
let sum = 0;
console.log({sum})
console.log({position: "starting loop", "arr.length-1": arr.length-1})
  for (let i = 0 ; i <= arr.length-1 ; i++){
console.log({i, sum, "arr[i]": arr[i]})
    return sum += arr[i];
console.log({sum})
  }
console.log("end loop")
console.log({sum})
}

const result = sumAll([1, 4]);
console.log(output)
1 Like

Okay I see the logic.

1 Like

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