Objective: To teach Javascript using code snippets with explanations for practical use. There will be random consistent updates made. I will try to answer questions in the comments section.
Note: These are not theory based lessons. They are being shared for those that learn by looking at practical detailed samples, then doing.
How to use these lessons:
-
- When you see “//” followed by words, it is because it allows – generally text and not code – to be displayed for the author/s and is usually an explanation of the code.
- When you see “console.log( );” It is a function that displays output to the screen so that you can see it. In most cases exist to show that the code is working as expected and not required.
- Most of the snippets that you see here can be seen in real-time in any modern browsers console.
How to sum all of the numbers in an array range
function sumAll(arr) {
//set the variable holder for the reply, return
let sum = 0;
//run a for loop that identifies the lowest and largest values or an array x and y;
for (let i = Math.min(...arr); i <= Math.max(...arr); i++){
sum += i; //sum the values into a total.
}
return sum; //returns the value of sum.
}
console.log(sumAll[1,4]);// Displays the expected results inside of the broswer console.
sumAll([1, 4]);