Have you ever tried to develop an algorithm for solving a problem in a technical interview? In this short tutorial, we'll tackle three main questions about algorithm design, starting with brute force (step by step, but not necessarily efficiently) and moving on to a more streamlined, elegant solution.
Line spread
Task
Having received the string, you need to expand it.
Solution # 1
string.substring(), str . .
— , . , myString.substring(1), .
() . . , .
, , string.charAt(). charAt : , .
.
// №1: Substring
function reverseString(str) {
let reversedString = '';
/* str
, i str.length
str, , .
*/
for (let i = str.length; i > 0; i--) {
reversedString += str.substring(i, i-1);
}
return reversedString;
}
// №2: CharAt
function reverseString(str) {
let reversedString = '';
/* str
i str.length-1 , i 0.
, ,
*/
for (let i = str.length-1; i >= 0; i--) {
reversedString += str.charAt(i);
}
return reversedString;
}
№2
— , .
:
string.split() — .
string.reverse() — .
string.join() — .
.
function reverseString(str) {
return str.split('').reverse().join('');
}
.
№1
string.split(' ') . , .
. . , , ! , .
for array.forEach(). , , .
// for
function findLongestWordLength(str) {
let maxVal = 0;
const wordArr = str.split(' ');
for(let i = 0; i < wordArr.length; i++) {
let word = wordArr[i];
if (word.length > maxVal) {
maxVal = word.length;
}
}
return maxVal;
}
// array.forEach
function findLongestWordLength(str) {
let maxVal = 0;
const wordArr = str.split(' ');
wordArr.forEach(word => {
if (word.length > maxVal) {
maxVal = word.length;
}
});
return maxVal;
}
№2
, string.split(), .
array.map(), . , .
arrOfLengths.
, Math.max(...spreadOperator) spread , .
function findLongestWordLength(str) {
const arrOfWords = str.split(' ');
const arrOfLengths = arrOfWords.map(item => item.length);
return Math.max(...arrOfLengths);
}
, . 4 .
[1,2,3,4]
[5,18,0,12]
[3,5,12,5]
[28,9,2,34]
Should return => [4,18,12,34]
№1
for-loop.
, .
// for
function largestOfFour(arr) {
let arrayOfMaxValues = [];
for (let i = 0; i < arr.length; i++) {
let subArr = arr[i];
let maxSubArrVal = 0;
for (let j = 0; j < subArr.length; j++) {
let currentValue = subArr[j];
if (currentValue > maxSubArrVal) {
maxSubArrVal = currentValue;
}
}
arrayOfMaxValues.push(maxSubArrVal);
}
return arrayOfMaxValues;
}
// forEach
function largestOfFour(arr) {
let arrayOfMaxValues = [];
arr.forEach(subArr => {
let maxSubArrVal = 0;
subArr.forEach(item => {
if (item > maxSubArrVal) {
maxSubArrVal = item;
}
});
arrayOfMaxValues.push(maxSubArrVal);
});
return arrayOfMaxValues;
}
№2
Math.max(...spreadOperator) array.map() , .
function largestOfFour(arr) {
return arr.map(subArr => Math.max(...subArr));
}
"Breaking Down JavaScript Solutions To Common Algorithmic Questions (Part 1)" by Emma Bostian
. , — !
I broadcast this and many other useful articles for beginner Frontend developers on the Frontend.school () Telegram channel , where I also prepare useful quizzes to test my knowledge. I draw your attention to the fact that the channel is purely a hobby and a desire to help and does not bring material benefits for me.