Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions projects/001-total-the-values/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Caso base: condizione di uscita dalla ricorsione
e
Passo ricorsivo
Richiamo la mia funzione fintantoche non raggiungo il caso base. Nel mio esercizio la condizione di uscita è una stringa vuota
*/
const prompt = require('prompt-sync')();
// function getSum() {
// const value = prompt('Enter a value');
// // Caso base: se l'utente digita enter
// if (value === '') {
// return 0;
// }
// // Adesso c'è il passo ricorsivo - Adesso prendo il valore e prendo il return
// return Number(value) + getSum();
// // Se ricevo una stringa vuota, esco dalla ricorsione
// // altrimenti prendo
// }
// console.log(getSum());
/**
* The function prompts the user to enter some numbers and interrupt by pressing enter on the keyboard
* @returns the sum of the numbers entered by the user
*/
function getSum() {
const value = prompt('Enter a value');
return value === '' ? 0 : Number(value) + getSum();
}
console.log(getSum());