Skip to content

Instantly share code, notes, and snippets.

@ibsenjg
Last active March 3, 2018 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibsenjg/b42807fe92b4ee88654ddc41a9c04d4e to your computer and use it in GitHub Desktop.
Save ibsenjg/b42807fe92b4ee88654ddc41a9c04d4e to your computer and use it in GitHub Desktop.
frontEndTest

Question 1

in the next expression

function validateString(str) {
   if (!str.toLowerCase().indexOf('superman') {
   throw new Error('String does not contain superman');
   }    
}

it has two issues, first need a ')' and the principal issue is that the expression '!str.toLowerCase().indexOf('superman')' returns true when the QA test a String like 'superman... " because the when the value is in their first place the function .indexOf() return the value '0' it means that the value is in the firs index of the word, basically is a number but when uses the operator '!' the number changes his kind of data to a boolean, that means when every testing value returns a zero before indexOf() it will transform to a boolean, this way everytime that '!str.toLowerCase().indexOf('superman') === true' will run the inside of the if statement triggering the exception. the solution is change the comparation inside the if, because the indexOf() function has a return value when the word doesnt exist on the string, the value is '-1', else, it will return a >=0 value.

function validateString(str) {
  if ( str.toLowerCase().indexOf("superman") === -1  ){
    throw new Error('String does not contain superman');
  }    
}

Q2

find a number in a sorted array without keys, using a for loop comparing the current number of the array (sorted) with the number i need to find, when it gets a truty value returns true, else the for will end and the function itself will return false.

function myFunction(a,n) {
	for (i = 0; i < a.length ; i++) {
		if (a[i] === n) return true; 
	}
	return false;
}

Q3

function myFunction(val,char='-') {
  var str = val.toString();
  var newchar = ''
  str = str.split('(').join(newchar).split(')').join(newchar).split('-').join(newchar);

  var res = str.substring(0, 3)+char+str.substring(3,6)+char+str.substring(6,10);

  return res;
}

Q5

function myFunction(initialArray,excludeArray) {
    
  for(var i = 0; i < exclude.length ; i++) {
    var choice = exclude[i].trim();
    for(var j = 0 ; j < files.length; j++) {
      if(files[j].trim() === choice) {
         files.splice(j, 1);
         j--;
      }
    }
  }

  for(var i = 0; i < exclude.length ; i++) {
    var choice = exclude[i].trim();
    for(var i = 0 ; i < files.length; i++) {
    var value = files[i].trim();
    var cut = value.substring(0,choice.length);
      if( cut === choice ) {
             files.splice(i, 1);
             i--;
      }
    }
    return files;
  }
}

Q7

starting from the idea that the given code runs by any way. within the fist instruction the constant get a value from the dom, creating a node array with N number of items which have that (argument) class, thanks to the .querySelectorAll()

then iterate the new array of nodes adding an event when each button gets clicked, the function that will triggers is a console.log() within an ES6 syntax message (template string) where you can join string with an expression that can be a variable without having to concat.

the message that will be printed when the first button get clicked "You clicked button #0", the message that will be printed when the fourth button get clicked "You clicked button #3". those numbers are because the node array index starts from zero.

Q8

function isIterable(x) {
	try {
	  var res = x.length ? true: false;
	} catch (err) {
	  return false;
	}
	  return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment