Simple JavaScript tasks

Simple JavaScript tasks

This is my first post!

·

3 min read

Table of contents

No heading

No headings in the article.

Hello!

This is my first post, I thought that for a start I will solve some simple JavaScript tasks I found online as a warm up.

Task1:

With given array: const numbers = [2, 5, 7, 10, 34, 16, 879, 1]. Write function that will display in console only even numbers from given array.

So as I remembered there is remainder operator (%) that returns remainder of division. I will base my function on that.

First I created empty array to store all even numbers as result of my function.

Then simple loop to iterate through given array.

Next I need if statement with condition to check if my number is even. When you divide even number by 2 the remainder should be equal to 0. So we use our operator (%) and divide our number by 2.

When if statement is true (number is even), we push the number to new array evennumbers.

Last we return our evennumbers array.

const numbers = [2, 5, 7, 10, 34, 16, 879, 1];

function onlyEvenNumbers(){
  let evennumbers = [];
  for (let number of numbers)
    if(number % 2 == 0) evennumbers.push(number);
  return evennumbers;
}

To show how this function works I simply used:

console.log("Filtered even numbers: "+ onlyEvenNumbers());

Task2:

Write sayHello function that takes a single argument, which is an object that contains the data of the person. If the passed object has the property name, your function should output “Hello, name!” To the console. If not, the function is supposed to print only “Hello!”. An example of an object to call a function is const person = {name: "Jill", age: 25, hobby: "sports"}.

Here in this task we only need to check if we have property name.

Recently I learned that it is best practice to use single if statements. And if possible avoid nesting them and using else. So I tried it also here.

const person = {name: "Jill", age: 25, hobby: "sports"}

function sayHello(newperson){
  if(!newperson.name) return "Hello!";
  return "Hello, " + newperson.name;
}

As you can see I first checked if we do not have the property name by using ! operator and then we return just "Hello!" If we have property name if statement will not execute and we will return "Hello, " + name

Then to display result I used:

console.log(sayHello(person));

Task3:

Write a simple randomizing machine, a function that takes an array as an argument that contains a list of people. Your function should return a random person from that array. Sample array to call the function: const students = ["John", "Bill", "Emma", "Stella", "Rob"].

So in my approach I remembered about Math.random() function that returns random number in range 0 to 1.

This time I just needed a random number from 0 to array length. As I assumed given array can vary in length.

So to explain this statement I will start from the end:

Math.floor(Math.random() * studentsarray.length)

firts we check given array length and multiply it by Math.random(). This will give us a number in range from 0 to array length. But this number can be for example 3.11245 so that is why we need Math.floor() to round it to integer number, to match position in our array.

const students = ["John", "Bill", "Emma", "Stella", "Rob"]

function randomStudent(studentsarray){
  return studentsarray[Math.floor(Math.random() * studentsarray.length)];
}

To display function result I used:

console.log(randomStudent(students));

And that is it. If you found any mistakes or you have ideas how to refactor my solutions to be simplier/ better please let me know :)

Did you find this article valuable?

Support knowledge blog by becoming a sponsor. Any amount is appreciated!