From bac35dce9eda8e41c8e75785b2bc84692e46eba1 Mon Sep 17 00:00:00 2001 From: zeliha pala Date: Sun, 12 Nov 2023 23:40:23 +0000 Subject: [PATCH] debug done --- week-2/debug/0.js | 5 ++++- week-2/debug/1.js | 5 +++-- week-2/debug/2.js | 25 +++++++++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/week-2/debug/0.js b/week-2/debug/0.js index b46d471a..fa6f1c64 100644 --- a/week-2/debug/0.js +++ b/week-2/debug/0.js @@ -1,7 +1,10 @@ // Predict and explain first... function multiply(a, b) { - console.log(a * b); + return a * b; } console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); +//the console does not return any value without return it will return undefined + +// If we want a function to return a value, we should use the return keyword. diff --git a/week-2/debug/1.js b/week-2/debug/1.js index df4020ca..6c9d3861 100644 --- a/week-2/debug/1.js +++ b/week-2/debug/1.js @@ -1,8 +1,9 @@ // Predict and explain first... +//The semicolon (;) following the return statement causes an immediate termination of the function. This semicolon prevents the continuation of expressions within the block and disregards the rest of the function. function sum(a, b) { - return; - a + b; + //return; a + b; } so it will be like this + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); diff --git a/week-2/debug/2.js b/week-2/debug/2.js index bae9652a..5fded648 100644 --- a/week-2/debug/2.js +++ b/week-2/debug/2.js @@ -1,14 +1,31 @@ // Predict and explain first... -const num = 103; +//Constant (const) declaration of 103 +//return converts the value of the num variable to a string (using the toString() method), and then takes the last character of the string. +// console.log gives last digit of any number. +// This program should tell the user the last digit of each number... +// Explain why getLastDigit is not working properly - correct the problem... + +/* The issue is that the function is not using the parameter passed to it. It always returns the last digit of the constant num, which is set to 103. The function should take a parameter and return the last digit of that parameter. */ + +/* + +const num = 103; function getLastDigit() { return num.toString().slice(-1); } console.log(`The last digit of 42 is ${getLastDigit(42)}`); console.log(`The last digit of 105 is ${getLastDigit(105)}`); -console.log(`The last digit of 806 is ${getLastDigit(806)}`); +console.log(`The last digit of 806 is ${getLastDigit(806)}`); */ + +//here is my solution ; -// This program should tell the user the last digit of each number. -// Explain why getLastDigit is not working properly - correct the problem +function getLastDigit(number) { + return number.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +console.log(`The last digit of 806 is ${getLastDigit(806)}`);