- Sum of range
function range(start, end)
{
let result = [];
result.push(start);
for (let i = 1; i < end - start + 1; i++)
{
result.push(start + i);
}
return result;
}
// Test
console.log(range(1, 10));
console.log(range(2, 5));
function sum(tab)
{
let result = 0;
for (let i = 0; i < tab.length; i++)
{
result += tab[i];
}
return result;
}
// Test
console.log(sum(range(1, 10)));
function rangeBonus(start, end, step = 1)
{
let result = [];
for (let i = 0; i < end - start + 1; i += step)
{
result.push(start + i);
}
return result;
}
// Test
console.log(sum(rangeBonus(1, 10)));
console.log(sum(rangeBonus(1, 10, 2)));
- Reversing an array
function reverseArray(tab)
{
let result = [];
for (let i = 0; i < tab.length; i++)
{
result.push(tab[tab.length - 1 - i]);
}
return result;
}
// Test
chiffres = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
inverseChiffres = reverseArray(chiffres);
function afficheTableau(tab, ordre)
{
let phrase = "Tableau des chiffres dans l'ordre " + ordre + " :";
console.log(phrase);
console.log("-".repeat(phrase.length));
if (ordre.toUpperCase() === "CROISSANT") console.log(tab);
else if (ordre.toUpperCase() === "décroissant".toUpperCase()) console.log(reverseArray(tab));
else console.log('Error!');
}
afficheTableau(chiffres, "croissant");
console.log("\n\n");
afficheTableau(chiffres, "décroissant");
function reverseArrayInPlace(tab)
{
let temp = 0;
for (let i = 0; i < tab.length / 2; i++)
{
temp = tab[i];
tab[i] = tab[tab.length - i - 1];
tab[tab.length - i - 1] = temp
}
return tab;
}
// Test
function afficheTableau(tab, ordre)
{
let phrase = "Tableau des chiffres dans l'ordre " + ordre + " :";
console.log(phrase);
console.log("-".repeat(phrase.length));
if (ordre.toUpperCase() === "CROISSANT") console.log(tab);
else if (ordre.toUpperCase() === "décroissant".toUpperCase()) console.log(reverseArrayInPlace(tab));
else console.log('Error!');
}
chiffres = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
afficheTableau(chiffres, "croissant");
console.log("\n");
afficheTableau(chiffres, "décroissant");
console.log(chiffres === reverseArrayInPlace(chiffres));
// Book's solutions
function reverseArray(array) {
let output = [];
for (let i = array.length - 1; i >= 0; i--) {
output.push(array[i]);
}
return output;
}
function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}