Лекция
Привет, сегодня поговорим про ответы на собеседование по javascript, обещаю рассказать все что знаю. Для того чтобы лучше понимать что такое ответы на собеседование по javascript, php на английском , настоятельно рекомендую прочитать все из категории Выполнение скриптов на стороне сервера PHP (LAMP) NodeJS (Backend) .
1. PHP & JavaScript
Complete the solution so that the function will break up camel casing, using a space between words.
Example
solution('camelCasing') should return 'camel Casing'
Your Solution:
// complete the function, PHP
function solution($string) {
//...
}
// complete the function,JavaScript
function solution(string) {
//...
}
// complete the function, PHP
function solution($string) {
$newstr='';
for ($i=0;$i<strlen($string);$i++)
{
if (ctype_upper($string[$i]))
$newstr.=' '.$string[$i];
else $newstr.=$string[$i];
}
return $newstr;
}
echo solution("camelCasing");
// complete the function,JavaScript
function solution(string)
{
string=string.split ("");
var i=-1;
var newstr="";
while(++i < string.length)
{
if ( string[i]==string[i].toUpperCase() )
newstr+= " "+string[i];
else newstr+= string[i];
}
return newstr;
}
console.log(solution('camelCasing') )
2. JavaScript
Description:
It's time to create an autocomplete function! Yay!
The autocomplete function will take in an input string and a dictionary array and return
the values from the dictionary that start with the input string. If there are more than 5 matches,
restrict your output to the first 5 results. If there are no matches, return an empty array.
Example:
autocomplete('ai', ['airplane','airport','apple','ball']) = ['airplane','airport']
The dictionary will always be a valid array of strings. Please return all results in the order
given in the dictionary, even if they're not always alphabetical. The search should NOT be case
sensitive, but the case of the word should be preserved when it's returned.
For example, "Apple" and "airport" would both return for an input of 'a'. However, they should
return as "Apple" and "airport" in their original cases.
Important note: Any input that is NOT a letter should be treated as if it is not there.
For example, an input of "$%^" should be treated as "" and an input of "ab*&1cd" should be treated as "abcd".
Your Solution:
// complete the function, JavaScript
function autocomplete(input, dictionary){
//...
}
// complete the function, JavaScript
function autocomplete(input, dictionary)
{
var num=0; result=[];
input=input.toUpperCase().replace(/[^a-zа-я]/gi,'');;
dictionary.forEach(
function outputItem(item, i)
{
if(item.toUpperCase().indexOf(input) + 1==1 && num<5) {result.push(item);num++;}
}
);
return result;
}
console.log(autocomplete('ai', ['airplane','airport','apple','ball']));
3. JavaScript & PHP
Description:
In elementary arithmetic a "carry" is a digit that is transferred from one column of digits
to another column of more significant digits during a calculation algorithm.
This is about determining the number of carries performed during the addition of multi-digit numbers.
You will receive an input string containing a set of pairs of numbers formatted as follows:
123 456
555 555
123 594
And your output should be a string formatted as follows:
No carry operation
3 carry operations
1 carry operations
Some Assumptions
Assume that numbers can be of any length.
But both numbers in the pair will be of the same length.
Although not all the numbers in the set need to be of the same length.
If a number is shorter, it will be zero-padded.
The input may contain any arbitrary number of pairs.
Your Solution:
// complete the function, PHP
function solve($input){
return "";
}
// complete the function, JavaScript
function solve(input){
return "";
}
// complete the function, PHP
function make_razrad($var)
{
$res=array();
$var = strrev(''.$var);
for($i = 0; $i < strlen($var); $i++)
{
$res[]=$var[$i];
}
return $res;
}
function cal($num1,$num2)
{
$num1=(int)$num1;
$num2=(int)$num2;
$num1=make_razrad($num1);
$num2=make_razrad($num2);
$carry=0;
$perenos=0;
$c1=count($num1);
$c2=count($num2);
$dig=max( $c1,$c2);
for ( $i=0;$i<$dig;$i++)
{
if ( $i>$c2 || $i>$c1) break;
if ($num1[$i]+$num2[$i]+$perenos>9){$carry++;$perenos=round(($num1[$i]+$num2[$i])/10);}
}
if ($carry==0)$carry='no';
return $carry;
}
function solve($input)
{
$tok = explode("\n",$input);
var_dump( $tok);
$res='';
foreach ($tok AS $t) {
$t=explode(" ",$t);
if (isset($t[0])&&isset($t[1]))
$res.=cal($t[0],$t[1])." carry operations \n";
}
return $res;
}
echo (solve("123 456
9 945678
555 555
123 594"));
// complete the function, JavaScript
function cal(num1,num2)
{
num1=parseInt(num1);
num2=parseInt(num2);
num1=make_razrad(num1);
num2=make_razrad(num2);
var carry=0;
var perenos=0;
var c1=num1.length;
var c2=num2.length;
var dig=Math.max(c1,c2);
for ( i=0;i<dig;i++)
{ num1[i]=parseInt(num1[i]);
num2[i]=parseInt(num2[i]); if ( i>c2 || i>c1) break ;
if (num1[i]+num2[i]+perenos>9){carry++;perenos=Math.round((num1[i]+num2[i])/10);}
}
if (carry==0)carry='no';
return carry;
}
function make_razrad(st)
{
res=[];st=st+'';
for(i = 0; i < st.length; i++)
{
res.push(st[i]);
}
return res.reverse();
}
function solve(input)
{
tok = input.split("\n");
res='';
tok.forEach(function(t) {
t=t.split(' ');
if ((t[0]!==null)&& (t[1]!==0)) res=res+cal(t[0],t[1])+" carry operations \n";
});
return res;
}
console.log(solve("555 555"));
4. Об этом говорит сайт https://intellect.icu . JavaScript & PHP
Description:
We will use the Flesch–Kincaid Grade Level to evaluate the readability of a piece of text.
This grade level is an approximation for what schoolchildren are able to understand a piece of text.
For example, a piece of text with a grade level of 7 can be read by seventh-graders and beyond.
The way to calculate the grade level is as follows:
(0.39 * average number of words per sentence) + (11.8 * average number of syllables per word) - 15.59
For example, in the following text:
The turtle is leaving.
The average number of words per sentence is 4 and the average number of syllables per word is 1.5.
The score is then (0.39 * 4) + (11.8 * 1.5) - 15.59 = 3.67
Write a function that will calculate the Flesch–Kincaid grade level for any given string.
Return the grade level rounded off to two decimal points.
HINT: Count the number of vowels as an approximation for the number of syllables.
But count groups of vowels as one (e.g. deal is one syllable).
Ignore hyphens, dashes, apostrophes, parentheses, ellipses and abbreviations.
Your Solution:
// complete the function, PHP
function fleschKincaid($text){
//...
}
// complete the function, JavaScript
function fleschKincaid(text){
//...
}
// complete the function, PHP
function count_vowels($string) {
$english_vowels = array('A', 'E', 'I', 'O', 'U');
$cnt=0;
$pred='';
for ($i=0;$i<strlen($string);$i++)
{
if ( in_array(strtoupper ($string[$i]) ,$english_vowels) && ($pred=='sogl'||$pred=='') ) $cnt++;
if ( in_array(strtoupper ($string[$i]) ,$english_vowels) ) $pred='glas';
else $pred='sogl';
}
return $cnt;
}
function make_sentence($string) {
return explode ('.',$string);
}
function makewords( $text) {
$text=trim(str_replace('.', " ",$text ));
$wordsarr= preg_split ('/\s+/' , $text);
return $wordsarr;
}
function avg_syllables_word($text)
{
$wordsarr= makewords( $text);
$cnt_wd=count($wordsarr);
$cnt_vow= 0;
foreach ($wordsarr AS $wd)
{
$cnt_vow+=count_vowels($wd);
}
return $cnt_vow/$cnt_wd;
}
function avg_word_in_sentence($text)
{
$cnt_s=count(make_sentence($text));
$cnt_w=count(makewords($text));
if($cnt_s>0)$cnt_s--;
return $cnt_w/$cnt_s;
}
function fleschKincaid($text) {
$text=trim(str_replace("-", "",$text ));
$text=trim(str_replace("(", " ",$text ));
$text=trim(str_replace(")", " ",$text ));
$text=trim(str_replace("'", "",$text ));
$text=trim(str_replace('"', " ",$text ));
$val= (0.39 * (avg_word_in_sentence($text) )+ (11.8 * avg_syllables_word($text)) - 15.59);
return round($val, 2);
}
echo fleschKincaid ('The turtle is leaving.');
sentence must be end only point.
// complete the function, JavaScript
function in_array(needle, haystack, strict) {
var found = false, key, strict = !!strict;
for (key in haystack) {
if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
found = true;
break;
}
}
return found;
}
function trim (str){return str.trim();}
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
function strlen( string ){
return string.length;
}
function count_vowels(string) {
var english_vowels = ['A', 'E', 'I', 'O', 'U'];
var cnt=0;
var pred='';
for (i=0;i<strlen(string);i++)
{
if ( in_array(string[i].toUpperCase() ,english_vowels) && (pred=='sogl'||pred=='') ) cnt++;
if ( in_array(string[i].toUpperCase() ,english_vowels) ) pred='glas';
else pred='sogl';
}
return cnt;
}
function make_sentence(string) {
return string.split('.') ;
}
function makewords( text) {
var str=text.replace('.','');
var mas = [], j = 0;
for (i = 0; i < str.length; i++) {
if (str[i] == " ") { j++; continue; }
else {
mas[j] ? mas[j] += str[i] : mas[j] = str[i];
}
}
return mas;
}
function avg_syllables_word(text)
{
wordsarr= makewords( text);
cnt_wd=count(wordsarr);
cnt_vow= 0;
wordsarr .forEach(function(entry) {
cnt_vow+=count_vowels(entry);
});
return cnt_vow/cnt_wd;
}
function count(array)
{
return array.length;
}
function avg_word_in_sentence(text)
{
cnt_s=count(make_sentence(text));
cnt_w=count(makewords(text));
if(cnt_s>0)cnt_s--;
return cnt_w/cnt_s;
}
function fleschKincaid(text) {
text=text.replace('(','').replace(')','').replace('-','');
val= (0.39 * (avg_word_in_sentence(text) )+ (11.8 * avg_syllables_word(text)) - 15.59);
return parseFloat(val).toFixed(2);
}
console.log(fleschKincaid('The turtle is leaving.'));
5. JavaScript & PHP
Description:
Write a function to reverse digits of a number.
You will receive an input number, example: 12345.
Expected result: 54321.
Please don't convert input number into the string.
Also, we would like to see two kinds of solutions, with and without recursion.
//JavaScript without recursion.
function reverseNumber (num) {
for(var r = 0; num; num = Math.floor(num / 10)) {
r *= 10;
r += num % 10;
}
return r;
}
//JavaScript with recursion. (don't convert input number into the string)
function reverseNumber (num) {
var res=''; function reversing(x){
y=x%10;
x=parseInt(x/10);
res=res+''+y;
if(x!=0){
reversing(x);
}
}reversing(num);
return parseInt (res);
}
// PHP without recursion.
function reverseNumber($n) {
$reverse = 0;
while ($n > 0)
{
$reverse = $reverse * 10;
$reverse = $reverse + $n%10;
$n = (int)($n/10);
}
return $reverse;
}
echo reverseNumber(123);
/ /PHP with recursion. (don't convert input number into the string)
function reverseNumber ($num)
{
$res='';
function reversing($x,$res){
$y=$x%10;
$x=(int)($x/10);
$res=$res.$y;
if(round($x)!=0)
{
reversing($x,&$res);
}
}
reversing($num,&$res);
return (int)$res;
}
echo reverseNumber(234);
Надеюсь, эта статья про ответы на собеседование по javascript, была вам полезна, счастья и удачи в ваших начинаниях! Надеюсь, что теперь ты понял что такое ответы на собеседование по javascript, php на английском и для чего все это нужно, а если не понял, или есть замечания, то не стесняйся, пиши или спрашивай в комментариях, с удовольствием отвечу. Для того чтобы глубже понять настоятельно рекомендую изучить всю информацию из категории Выполнение скриптов на стороне сервера PHP (LAMP) NodeJS (Backend)
Ответы на вопросы для самопроверки пишите в комментариях, мы проверим, или же задавайте свой вопрос по данной теме.
Комментарии
Оставить комментарий
Выполнение скриптов на стороне сервера PHP (LAMP) NodeJS (Backend)
Термины: Выполнение скриптов на стороне сервера PHP (LAMP) NodeJS (Backend)