Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Question 1
- ##### Cause of Error
- Since the if statement evalutes to a 0 if the word superman is the first word, this evaluates to false and the not operator
- evalutes it to a true, hence the error is thrown.
- ##### Solution
- Adding a 1 to the 0 will eliminate the if statement evaluating a 0 and thus the sentence 'Superman is awesome!' does not give an error.Regardless of the index for the word superman. Another solution is to comparison operator with a negative value.
- ##### Code
- function validateString(str) {
- if (!(str.toLowerCase().indexOf('superman') + 1)) {
- throw new Error('String does not contain superman');
- }
- }
- function validateString(str) {
- if (!(str.toLowerCase().indexOf('superman') !== -1)) {
- throw new Error('String does not contain superman');
- }
- }
- ### Question 2
- ##### Assumptions
- 1. Array is sorted completely
- 2. Array comprises of only integers e.g. [0,1,2,3,4]
- ##### Solution
- What we can use here is a simple binary search that returns a true or false depending on the presence of the demanded integer in the array.
- Binary Search divides up the array in to 2 halfs to look for integer 'x'. if 'x' is in the lower half we
- reduce the upper bound (and viceversa) using the middle index dependant on start and end values for the algorithm to sweep through the appropriate part of the array to check for integer 'x'.
- This is a recursive function that either increases or decreases the start/end indices depending on the location of 'x' in the array with a time complaxity of
- O(lgn).
- ##### Code
- function binarySearch (arr, x, l, r) {
- // Base Condtion
- if (l > r) return false;
- // Find the middle index
- let mid = Math.floor((l + r)/2);
- // Compare mid with given key x
- if (arr[mid]===x) return true;
- // If element at mid is greater than x,
- // search in the left half of mid
- if(arr[mid] > x)
- return binarySearch(arr, x, l, mid-1);
- else
- // If element at mid is smaller than x,
- // search in the right half of mid
- return binarySearch(arr, x, mid+1, r);
- }
- ### Question 3
- function fixPhoneNumber(value, delimiter = '-') {
- let number = '';
- value = value.trim();
- for (el of value) {
- if (!isNaN(el) && el !== ' ') number += el;
- }
- if (number.length !== 10 || !number) {
- throw Error('Number does not have sufficient disgits');
- }
- return `${number.substr(0,3)}${delimiter}${number.substr(3,3)}${delimiter}${number.substr(6)}`
- }
- /*Phone Numbers with extensions will not pass this function or numbers that
- do not follow US phone number formats*/
- ### Question 4
- const assert = require('assert')
- const fizzBuzz = require('./fizBuzz');
- describe('fizzBuzz', function() {
- it('should render an error with invalid start and stop values', function() {
- const start = 1;
- const stop = 0
- assert.throws(() => fizzBuzz(start, stop), Error, 'Invalid arguments')
- })
- it('should return 1 for equal stop and start', function() {
- const start = 1;
- const stop = 1;
- const result = fizzBuzz(start, stop);
- assert.equal('1', result);
- });
- it('should return valid result with stop value of 2', function() {
- const start = 1;
- const stop = 2;
- const res = fizzBuzz(start, stop);
- assert.equal('2', res[1]);
- });
- it('should return valid result with stop value of 4', function() {
- const start = 1;
- const stop = 4;
- const res = fizzBuzz(start, stop);
- assert.equal('12Fizz4', res)
- })
- it('should return valid result with stop value of 7', function() {
- const start = 1;
- const stop = 7;
- const res = fizzBuzz(start, stop);
- assert.equal('12Fizz4BuzzFizz7', res)
- })
- it('should return valid result with stop value of 10', function() {
- const start = 1;
- const stop = 10;
- const res = fizzBuzz(start, stop);
- assert.equal('12Fizz4BuzzFizz78FizzBuzz', res)
- })
- it('should return valid result with stop value of 15', function() {
- const start = 1;
- const stop = 15;
- const res = fizzBuzz(start, stop);
- assert.equal('12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz', res)
- })
- it('should return valid result with stop value of 20', function() {
- const start = 1;
- const stop = 20;
- const res = fizzBuzz(start, stop);
- assert.equal('12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19Buzz', res)
- })
- it('should return valid result with stop value of 200 by giving a count value', function() {
- const start = 1;
- const stop = 200;
- const res = fizzBuzz(start, stop).length;
- assert.equal(687, res)
- })
- it('should return valid result with default params', function() {
- const res = fizzBuzz().length;
- assert.equal(313, res)
- })
- it('should return valid result with valid start param', function() {
- const start = 10;
- const res = fizzBuzz(start, undefined).length;
- assert.equal(292, res)
- })
- it('should return valid result with valid stop param', function() {
- const stop = 10;
- const res = fizzBuzz(undefined, stop).length;
- assert.equal(25, res)
- })
- })
- ### Question 5
- function filterFiles (files, exlcudeFiles) {
- return files.filter(dir => !exlcudeFiles.find(path => path === dir || dir.indexOf(path) === 0))
- };
- ### Question 6
- For this question I googled the algorithm as my solution did not render optimal results.
- function getColor(name) {
- let hash = 0;
- for (let i = 0; i < name.length; i++) {
- hash = name.charCodeAt(i) + ((hash << 5) - hash);
- }
- let colour = '#';
- for (let i = 0; i < 3; i++) {
- let value = (hash >> (i * 8)) & 0xff;
- colour += ('00' + value.toString(16)).substr(-2);
- }
- return colour;
- }
- ### Question 7
- Since the variable i is initialized to 0, when the buttons are clicked we would get the following:
- You clicked button #0
- You clicked button #3
- ### Question 8
- const isIterable = (obj) => {
- try {
- return typeof obj[Symbol.iterator] === 'function'
- }catch(e){
- return false
- }
- }
- I googled this one. My solution was inoptimal as I used for loops and if statement to check what comes in e.g. array, onject etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement