Skip to content

ZA-ITP-May-2025 | Christian Mayamba | Module-Structuring and Testing Data | Week 2 #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Mandatory-debug-2 solved
  • Loading branch information
c-mayamba committed Jun 20, 2025
commit c5e6d6a4f31760fbb8fbe94cbc055cca28a0876a
23 changes: 17 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Since 'num' is defined as a global variable,
// the function getLastDigit will always return the last digit of 103.

const num = 103;

Expand All @@ -14,11 +15,21 @@ console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// =============> write the output here:
// "The last digit of 42 is 3"
//"The last digit of 105 is 3"
//"The last digit of 806 is 3"

// Explain why the output is the way it is
// =============> write your explanation here
// =============> The output is always 3 because the variable `num` is defined globally as 103,
// and the function `getLastDigit` is not using any parameter.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> Here's the corrected code:
function getLastDigit(num) {
return num.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
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)}`);