You get a bonus - 1 coin for daily activity. Now you have 1 coin

questions and answers to the interview on JavaScript and PHP in English

Lecture



  questions and answers to the interview on JavaScript and PHP in English

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!

Dictionary dictionary and return

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']

Will always be a valid array of strings. Please return all results in the order

given in the dictionary, even if not always alphabetical. The search should NOT be case

If you 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: It should not be a letter.

For example, it should be treated as "input" 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 – th ] / 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

digits during a calculation algorithm.

This is a number of multi-digit numbers.

You will receive an input 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 will be of the same length.

Although it is not necessary.

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. JavaScript & PHP

Description:

We will use the Flesch – Kincaid Grade Level to evaluate the readability of a piece of text.

This is a level of schoolchildren.

For example, you can read the seventh-graders and beyond.

The following is the way to calculate the level:

(0.39 * average number of words sentence) + (11.8 * average number of syllables per word) - 15.59

For example, in the following text:

The turtle is leaving.

The number of words per sentence is 4 and the average number of words per word is 1.5.

The score is then (0.39 * 4) + (11.8 * 1.5) - 15.59 = 3.67

Write a function for any given string.

Return decimal points rounded off to two decimal points.

HINT: Count the number of vowels as an approximation for the number of syllables.

Butt groups of vowels as one (eg 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 do not convert input number into the string.

Also, we would like to 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);


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)