Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.
In this post I am going to discuss two challenges. Both are pretty easy but worth analyzing, especially for beginners.
The first challenge starts off with two arrays, both are filled with 5 random numbers. Then it creates a new array
arr which is
= a1.concat(a2). The concat operation takes the values from
a1 and appends the values of
a2, basically it combines both arrays. In the end
arr will contain 10 numbers (first all from
a1 then all from
a2). That's what concatenation means. So to solve this challenge we need to solve
R = arr.length which is 10.
The second challenge is slightly more difficult. This time the challenge asks us to fix the bug 🐼. It also states that
A = 17576 which is a pretty large number, and the
tricube function looks frightening to a beginner. But fortunately you can ignore this information. The bug 🐼 appears to be just a variable name, the only meaningful variable at that scope is
Let's briefly discuss the last three lines of code.
let A = tricube(3); This creates variable A and gives it the value that's returned from the function.
A = Math.floor(A); this rounds down the value of A, for instance 0.5 becomes 0, 5.2 becomes 5, 7.9 becomes 7. That's how floor-rounding works.
A = Math.abs(A); This ensures that A is absolute (non-negative).
By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/