Ein Baum und seine Abenteuer



Invert strings

Getting close to finishing my Masters I recently started to interview for employment opportunities. As I have a Computer Science background this involves doing some programming. After all potential employers want to find out if I can code. These practical tests need to be short yet tell you something about the applicants skill level. One challenge I faced was to invert a string. And as JavaScript is my favorite programming language here are four way to do that in JS.

For loop

The always reliable fall that will surely work. For loops are a basic concept in programming. Pay special attention how this loop counts down instead of up.

function forLoopInvert(string) {
	let result = ''
	for(let i = string.length - 1; i >= 0; i--) {
		result += string.charAt(i)
	}
	return result
}

Reverse and array

Since arrays in JavaScript come with a build in ´reverse´ function we can simply use that.

function arrayInvert(string) {
	return string.split('').reverse().join('')
}

Fancy Arrays things

While we create an array from our string we might as well use a more functional approach in the sense that we can use reduce to create a new string from the array.

function reduceInvert(string) {
	return string.split('').reduce((result, char) => {
		return char + result
	}, '')
}

Recursion

Ahh the height of programming: recursion. Where every Computer Science student gets to experience a stack overflow and popular websites get their name.

function recursiveInvert(string) {
	if(string.length > 1) {
		return string.substring(string.length - 1) + recursiveInvert(string.substring(0, string.length - 1))
	} else {
		return string
	}
}

Conclusion

As always in programming you have many ways to solve the same problem. At an interview the fanciness of your code will come down to your nerves as well as proficiency at the language you use. So if in doubt make up methods things have and fall back on for loops 😉

Author

Portrait picture of Hendrik

I am a JavaScript and GenAI Enthusiast; developer for the fun of it!
Here I write about webdev, technology, personal thoughts and anything I finds interesting.

More about me

Read next

Trying my hands at Deno v1.0

Denos awesome Dinosaur logo.
Denos awesome Dinosaur logo.

Following the release of Deno v1.0 I got excited to try my hands at it. These are my first experiences writing a simple tool in Deno.

A super fast introduction to Deno: Deno is the spiritual successor of Node trying to fix design mistakes that were made early on but recognized only late into the project. Deno supports TypeScript out of the box and relies on web-standards. In Deno you can import ES modules from any URL and use fetch like you would in the browser. To help unify the community on processes and workflows Deno provides a wide array of stdLibs and has build in solutions for bundling, testing and code formatting. You can read more in the Deno v1 release post.

ES6 need to know

Let us take a good look at this thing they call ES6.
Let us take a good look at this thing they call ES6.

ES6 is out, browser support is actually decent and if that is not enough for you there is always Babel. So lets get up to speed with the most important and awesome new features.

Let there be Const

Scope in JavaScript come unfamiliar to most. Because we have a thing called "variable hosting" that pulls all variables out and defines them at the top of their Scope. Their Scope being either the global one or the first surrounding function. The classic example here is a for loop.

Prompting made me a better communicator

"Prompting bridges communication" - generated using Midjourney
"Prompting bridges communication" - generated using Midjourney

Prompt Engineering has become a passion topic of mine. It's super fun to tinker with GenAI, to ask it questions and to optimize the way that I ask those questions.

The basic of Prompt Engineering are simple:

  1. Task specify a task that you want the LLM to do for you.
  2. Context give the chatbot the context that it needs to complete your task.
  3. Format how do you want the LLM to respond to you? A bullet point list? A table? A slide deck? a blogpost?
  4. Process if you already know a great process to solve the problem, tell the AI to follow it. If you don't, at least give it room to think.