JavaScript Practice Exercises For Beginners: Beginner Exercises Part 2

preview_player
Показать описание

These JavaScript Practice exercises for beginners are mainly focused around string processing and manipulation. Being exercises for beginners, they're all fairly straightforward.

We'll be completing 5 JavaScript practice exercises and the first few can be solved with a one line arrow function and the slice function.

It can be handy to practice these JavaScript exercises yourself before seeing the solution so I encourage you to at least try and have an attempt at the JavaScript exercises before seeing how I would have solved them.

If you're a a JavaScript beginner, you'll learn about how to use arrow functions, implicit returns and a lot of string manipulation. Channel Handle @codewithbubb
Рекомендации по теме
Комментарии
Автор

Hi . thank you for these wonderful excercises. Although, i should point out that the ternary operator in your fourth excercise will return the wrong value if the input values are greater than 100, eg- 120 and 150, the function will output that 150 is closest to 100 than 120. we indeed need two if statements and one else for the correct output. if (Math.abs(num1 - 100) < Math.abs(num2 - 100)) return num1;
if (Math.abs(num1 - 100) > Math.abs(num2 - 100)) return num2;
else return "equal";

dfsfsdsfdfdn-mbyc
Автор

I feel so confident after completing part 1 and 2 of exercises. Can't wait to finish them all. Loving these videos.

motivatedDreamer
Автор

I wrote exercise 4 at this way:
const near100 = (a, b) =>
(Math.abs(a - 100) < Math.abs(b - 100) ? a : b);

romulonavas
Автор

thank you so much ...you are really helping peoples bless you brother from Algeria <3

titocorg
Автор

Alternative one-line solution for exercise 5:

const charCount = (str, char) => (str.split(char).length - 1) > 1 && (str.split(char).length - 1) < 5;

Using str.split(pass in char) calculates how many splits there are at the given character. Subtract 1 from this to get the true length.
I just had true or false returned, but using the ternary operator you could add which responses you want back. Hope this helps

RD-jrnv
Автор

Im happy that I done 4 exercises from 5 :D

panecosmin
Автор

It good and approaching the condition with ternary operator is quite simple

codeswithankit
Автор

You are awesome learnt a lot from you thanks for being a youtuber :)

ratul
Автор

7:46 exercise 4 solution do have some bugs if the value exceeds 100(For example==> (105, 99). 105 subtracted to 100 will result to a negative number thus the program thinks that it is always the closest because negative will be always less than the positive number. To address this, we must use Math.abs() to get the absolute value of a number. Also, for equal value inputs, included some tweaks in the code

Here is my other solution:

const nearest = (num1, num2) => num1!==num2 ? (Math.abs((100-num1)) > Math.abs((100-num2)) ? `${num2} is closer to 100` : `${num1} is closer to 100`) : `Your input values are equal, input another value`

I used ternary operator with a condition inside a condition 😂

elilumilay
Автор

Thank you for these videos...they have been incredibly helpful.

lelnine
Автор

*Me with a large 10 line if/else statement*: Oh this is a great solution to the problem posed!
*JDC*: OK now this is the solution *3 lines later*

HarteMusic
Автор

Amazing teaching at its best ::: noobProgrammer from India 🙏🏻

rhunuiipadprog
Автор

Omg where were u all these years ?
I just love your content so much.

Gagandeep-oucs
Автор

Q1)
function charMerger(str){
if(str.length > 3){
let preWord = str.slice(0, 3)
let newstr = `${str} `
let postWord = newstr.slice(-4, -1)
return `${preWord} ${postWord}`
}else{
return `${str}`;
}
}





mine approach is more better(not optimized but better) cause i tried your method but it print 3char words trice.
(btw im learning from you, so until now this is the only problem i solved other i learnt from you).

ganeshchougale
Автор

Hi there!
I’m a bloody beginner in programing and these exercises help me a lot to build my confidence in my programming skills. Especially after just reading the description of a practice app for beginners and me having no idea how to start writing it :/
I wanted to share my solution to exercise 5. Having no experience I just search online and try to fit the first thing I find to the exercise. So, here is what I came up with:

const oFinder = (str) => {
let allTheO = [];
for (let i = 0; i < str.length; i++){
if (str[i] === 'o') allTheO.push(i);
}
if (allTheO.length >= 2 && allTheO.length <= 4) return true;
else return false;
}

DilinarTanel
Автор

For question #2, I felt the fact that you explicitly said this function was meant to run for even numbers means that this should be clearly called out in the code so I used this:

const getFirstHalf = (str) =>
(str.length % 2 === 0) ? str.slice(0, str.length/2) : str

georgieadolphe
Автор

For ex 5 I foud a one-line solution :

const letterFrequency = (str, letter) =>
str.split('').filter((value => value === letter)).length >= 2
  && str.split('').filter((value => value === letter)).length  <=4

Lore-the-Soat
Автор

Thank you for the exercises :) I subscribed to your channel for more of this

elilumilay
Автор

Hi
I understood the exercise this way, this is my solution

let str1=('templated');
let str2=('oppened');
let newstr = str1.substring(1) +

console.log(newstr);

zohrabmazmanyan
Автор

Thank you for making such a great video. There's actually one more case to consider in exercise 4: if we pass a number that is greater than 100 we get the answer wrong. So we need to compare the absolute values of the differences using Math.abs(). I offer a simple one line solution :
const closeTo100 = (num1, num2) => Math.abs(100 - num1) > Math.abs(100 - num2) ? num2 : num1;

avagsargsyan