Embark on a journey of coding excellence with CodeKata, a curated series of challenges designed by industry veterans from companies like Microsoft, Walmart, Samsung, and more.
Practice on CodeKata to enhance your coding proficiency, level up your skills, and boost your chances of acing coding interviews.
Explore a range of coding challenges across categories like π° Absolute Beginner, π’ Mathematics, π Arrays, π€ Strings, and more. CodeKata caters to all skill levels, making it ideal for both novice and experienced coders.
Building on the basics covered in our introductory guide, dive into real-world problem-solving with JavaScript on CodeKata. Hone your skills, tackle challenges across domains, and elevate your coding prowess through practical exercises.
Now, let's dive into problem-solving on CodeKata. To get started, visit CodeKata on GUVI and explore a curated series of challenges across different categories. Whether you're an absolute π° beginner or an experienced π¨βπ» coder, CodeKata provides a range of tiles to suit every skill level and interest.
JavaScript is a language that is not inherently designed for obtaining basic user input. However, with the help of Node.js packages, we can facilitate the process of receiving user input.
To gather user input in JavaScript, you can use the following default code:
// Getting input via STDIN
const readline = require("readline");
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
// start-here
// Your code goes here β¦ replace the below line with your code logic
console.log(userInput);
// end-here
});
π° Note for Beginners:
Understanding the intricacies of this basic snippet may not be crucial at the start. Instead, focus on utilizing the data in the userInput
array to solve problems in your CodeKata challenges. As you progress, you'll become more familiar with handling user input and can delve deeper into the code details.
-
const readline = require("readline");
- Import the
readline
module, a utility for reading data from a readable stream, using therequire
function.
- Import the
-
const inp = readline.createInterface({ input: process.stdin });
- Create an interface (
inp
) for reading input. Specify that the input should come from the standard input (stdin
) stream.
- Create an interface (
-
const userInput = [];
- Initialize an empty array called
userInput
to store the user's input.
- Initialize an empty array called
-
inp.on("line", (data) => { userInput.push(data); });
- Set up an event listener on the
line
event of theinp
interface. When a line of input is received, the provided callback function is executed, pushing the input data into theuserInput
array.
- Set up an event listener on the
-
inp.on("close", () => { console.log(userInput); });
- Set up another event listener on the
close
event of theinp
interface. When the input stream is closed (no more data to read), the provided callback function is executed. This is where you can place your code logic. In the example, it prints the collecteduserInput
array to the console.
- Set up another event listener on the
You are tasked with creating a program that determines whether a given number is odd or even.
There will be only one line of input, which contains a single integer.
Print "Even" if the number is even, and "Odd" if the number is odd.
4
Even
An integer is a number that can be represented as a whole number. In other words, it is a number without any fractional part. For example, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
An odd number is a number that is not divisible by 2. For example, 1, 3, 5, 7, 9, 11, 13, 15
An even number is a number that is divisible by 2. For example, 2, 4, 6, 8, 10
We can use the modulo operator (%
) to determine if a number is odd or even. If the remainder of the number after division by 2 is 0, it is even. Otherwise, it is odd.
// Assuming the first element in userInput is the number to check
const numberToCheck = parseInt(userInput[0]);
// Check if the number is odd or even
if (numberToCheck % 2 === 0) {
console.log("Even");
}
else {
console.log("Odd");
}
// Getting input via STDIN
const readline = require("readline");
const inp = readline.createInterface({
input: process.stdin
});
const userInput = [];
inp.on("line", (data) => {
userInput.push(data);
});
inp.on("close", () => {
// start-here
// Your code goes here β¦ replace the below line with your code logic
// Assuming the first element in userInput is the number to check
const numberToCheck = parseInt(userInput[0]);
// Check if the number is odd or even
if (numberToCheck % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
// end-here
});
-
const numberToCheck = parseInt(userInput[0]);
- This line assumes that the first element in the
userInput
array contains the number you want to check (as per the problem statement). It usesparseInt()
to convert the string representation of the number into an actual integer. This step is necessary because user input is initially collected as strings, and we want to perform numerical operations.
- This line assumes that the first element in the
-
if (numberToCheck % 2 === 0) {
- This line checks if
numberToCheck
is divisible evenly by 2. The%
operator gives the remainder after division. So,numberToCheck % 2 === 0
checks if the remainder is 0, indicating that the number is even.
- This line checks if
-
console.log("Even");
- If the condition in the previous line is true (i.e., the number is even), this line prints "Even" to the console.
-
} else {
- If the condition in the
if
statement is false (i.e., the number is not even), the code inside theelse
block will be executed.
- If the condition in the
-
console.log("Odd");
- This line prints "Odd" to the console, indicating that the number is not even.
So, in summary, the code takes user input, extracts the number to check, determines if it's even or odd using the modulo operator, and prints the result to the console. It's a simple and effective way to solve the Odd or Even Checker problem.
Let's solve more problems.
-
Print the
userInput
array:- This step helps in understanding the input format and allows us to visualize the data we're working with.
-
Spread values into variables:
- Assign meaningful variable names to different elements in the
userInput
array. For example,numberToCheck
andnumberToPrint
are good choices.
- Assign meaningful variable names to different elements in the
-
Convert input values to the desired data type:
- Since the input values are initially collected as strings, use appropriate conversion functions (
parseInt
,parseFloat
, etc.) to convert them to the desired data type.
- Since the input values are initially collected as strings, use appropriate conversion functions (
-
Proceed to solve the problem:
- Apply the necessary logic to solve the specific problem using the variables created. Break down the problem into smaller steps if needed.
Now that you've learned how to solve a problem, let's move on to the next problem in the series.
Welcome, coding enthusiast! π If you're just starting your coding journey, consider following these problem categories to enhance your skills and build a solid foundation:
- Input/Output
- Absolute Beginner
- Basics
- Maths
- Array
- String
- Looping
- Patterns
Thank you for taking the time to read my article. Your engagement is the driving force behind the words on these pages. Whether you found information, inspiration, or simply enjoyed the content, your presence is deeply appreciated. Writing is a shared journey, and I'm grateful to have you as a reader. Cheers to the joy of exploration and discovery! π
If you enjoyed the article, consider giving it more stars!
With gratitude,
Pugazharasan C