Skip to content

Instantly share code, notes, and snippets.

@hongphuc5497
Last active March 1, 2023 18:01
Show Gist options
  • Save hongphuc5497/f3cbef5c3368ee303f1036e67236dbdc to your computer and use it in GitHub Desktop.
Save hongphuc5497/f3cbef5c3368ee303f1036e67236dbdc to your computer and use it in GitHub Desktop.
Websparks Nodejs Test

Q1:

  • This calculator takes values that could be written in a browsers route path as a single string. It then returns the result as a number (or an error message).

  • Route paths use the '/' symbol so this can't be in our calculator. Instead we are using the '$' symbol as our divide operator.

  • You will be passed a string of any length containing numbers and operators:

    • '+' = add
    • '-' = subtract
    • '*' = multiply
    • '$' = divide
  • Your task is to break the string down and calculate the expression using this order of operations. (division => multiplication => subtraction => addition)

  • If you are given an invalid input (i.e. any character except .0123456789+-*$) you should return the error message:'400: Bad request'

  • Remember:

    • Operations are infinite
    • Order of operations is imperative
    • No eval or equivalent functions
  • Examples:

    calculate('1+1') => '2'
    calculate('10$2') => '5'
    calculate('1.5\*3') => '4.5'
    calculate('5+5+5+5') => '20'
    calculate('1000$2.5$5+5-5+6$6') =>'81'
    calculate('10-9p') => '400: Bad request'

Q2:

  • Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.

  • Examples:

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !
@hongphuc5497
Copy link
Author

function pigIt(str) {
	let result = '';

	str.split(' ').forEach((word) => {
		if (word.match(/^[a-zA-Z]+$/)) {
			result += word.substring(1, word.length) + word.substring(0, 1) + 'ay ';
		} else {
			result += word + ' ';
		}
	});

	return result.trim();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment