0

I had a test interview and for 3 questions I didn't know the answer:

  1. Write a function that will insert underscore between characters: this will become t_h_i_s.

  2. Write a function that will output this:

l('t') === 'lt'
l()('t') === 'l3t'
l()()('t') === 'l33t'
l()()('g') === 'l33g'
l()()()()()()()()()()()('t') === 'l33333333333t'
  1. Why the output is true?

var bar = true;
function foo() {
  bar = false;
  return 5;
  function bar() {}
}
foo();
console.log(bar);

Can someone help please with the answers?

John Smith
  • 99
  • 1
  • 1
  • 9
  • 5
    You need to ask 3 different questions – gurvinder372 Oct 11 '16 at 09:11
  • 2
    Do your homework. – Rax Weber Oct 11 '16 at 09:11
  • 2
    1. and 3. are trivial, have you even tried to research anything? – Jakub Jankowski Oct 11 '16 at 09:12
  • I searched on google believe me and I didn't find anything to solve them. – John Smith Oct 11 '16 at 09:16
  • It there are very easy for you guys why don't you put the answer, instead of writing something else. I know I'm a beginner and I ask here for answers not for saying me that I am a beginner. – John Smith Oct 11 '16 at 09:18
  • So you claim to have done some research which is good but what have you tried? Please update your question and display your attempt(s). You don't just get answers given, we need to see what you have tried so we have something to debug and explain why your current source code isn't functioning a intended, then and only then some people might offer a solution. – NewToJS Oct 11 '16 at 09:18
  • @NewToJS There's no requirement here that says you need to have tried something to receive answers. However, this *is* a site for *"professional or enthusiast programmers"*, which means we expect a minimal level of proficiency, which means that when you do run into a situation where you have a question, you must have tried *something*, and we'd like to see that *something* in order to give a more to-the-point answer instead of starting explaining Javascript from scratch. In this case, OP seems to be below "professional or enthusiast" level, and hence this may be the wrong site for them. – deceze Oct 11 '16 at 09:33

4 Answers4

3
  1. Write a function that will insert underscore between characters: this will become t_h_i_s.

You want to write a function that iterates over all characters in a string, and appends an underscore between all characters.

For example:

function underscoreString(str) {
    var result = str.charAt(0);

    for (var i=1; i<str.length; i++) {
        result += '_' + str.charAt(i);
    }

    return result;
}

console.log( underscoreString('this') );
  1. Write a function that will output this:

You will need to write a function that returns another function, so you can chain the functions. Since Javascript allows you to store functions as variables, you can make use of this by re-calling the same function continuously until a proper argument is returned.

The following function is an example. It works as intended but is not the most beautiful.

function l(ch) {
    var str = 'l';
    if (ch) return str + ch;
    else str += '3';

    var newFunc = function (ch) {
        if (ch) return str + ch; 
        str += '3';
        return newFunc;
    }
    return newFunc
}

console.log( l('t') === 'lt' );
console.log( l()('t') === 'l3t' );
console.log( l()()('t') === 'l33t' );
console.log( l()()('g') === 'l33g' );
console.log( l()()()()()()()()()()()('t') === 'l33333333333t' );
  1. Why the output is true?
var bar = true;
function foo() {
  bar = false;
  return 5;
  function bar() {}
}
foo();
console.log(bar);

The bar that is within the function foo() is not referencing the global variable bar. Instead, it is referencing the function function bar() {}. This is because of hoisting, as mentioned in the comments.

Thus, the global bar variable is not touched at all by the function, and stays true at all times.

2

It really depends on the expected level of code. If you need to demonstrate understanding of algorithms or knowledge of how to use javascript constructs.

For example, the first one could be as simple as:

function insertUnderscore(x){
    return x.split('').join('_');
}

2nd question a recursive method:

function l( end ){
    var acc = '';
    function iter( eChar ){
        if( typeof eChar === "undefined"){
            acc=acc+'3';
            return iter;
        }
        return 'l'+acc+eChar;
    }
    if(typeof end === "undefined"){
        acc = acc + '3';
        return iter;
    }
    return iter(end);
}

Third question: function bar(){} actually declares 'bar' within the local scope, so your assignment bar = false acts on local 'bar'.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
1

This one simply returns the iterator function if the letter is undefined, When the letter is defined it repeats the character '3' n times.

The other two should be pretty easy to figure out

function l(letter) {
  let count = 0
  function iter(letter) {
    if (typeof letter === 'undefined') {
      count++
      return iter
    } else {
      return 'l' + ('3'.repeat(count)) + letter
    }
  }
  return iter(letter)
}

console.log(l('t') === 'lt')
console.log(l()('t') === 'l3t')
console.log(l()()('t') === 'l33t')
console.log(l()()('g') === 'l33g')
console.log(l()()()()()()()()()()()('t') === 'l33333333333t')
synthet1c
  • 6,152
  • 2
  • 24
  • 39
1

Question 1

Use a negative lookahead for the beginning of the string and a positive lookahead for a character. Replace the given empty string with an underscore.

function spacer(s) {
    return s.replace(/(?!^.)(?=.)/g, '_');
}

console.log(spacer('this'));

Question 2

Use a closure and return for a non given paramter the function otherwise the extended value.

function l(v) {
    var s = 'l';
        fn = function (v) {
            s += 3;
            return v === undefined ? fn : s + v;
        };

    return v === undefined ? fn : s + v;
}

console.log(l('t') === 'lt');
console.log(l()('t') === 'l3t');
console.log(l()()('t') === 'l33t');
console.log(l()()('g') === 'l33g');
console.log(l()()()()()()()()()()()('t') === 'l33333333333t');

Question 3

Because function bar() {} is hoisted to the begin of the function and then overwritten with false. The outer bar variable has never changed it's content.

var bar = true;
function foo() {
    bar = false;
    console.log('foo\'s bar:', bar);
    return 5;
    function bar() {}
}

foo();
console.log(bar);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Thank you @Nina Scholz for you explanations. At 1 must be a function, nothing else. The other guys show me how to do it. – John Smith Oct 12 '16 at 10:10