Home > OS >  Function with output "happy birthday"
Function with output "happy birthday"

Time:09-04

The idea is to create a function with the simple output(log) "happy birthday", without this word being present in the function. Can anyone give me some insights on how I can accomplish this?

I am still learning to program. I thought about using a list of strings called "vowels" that has the value ['a', 'e', 'i', 'o', 'u'] and others called "consonants"... But I can't figure out how to make that work in my mind using just some kind of counter.

Thank you all.

CodePudding user response:

Many ways to do this. You could try using an array of alphabet letters and returning the matching array indexes.

function renderHappyBirthday () {
// this is an array of all the letters in the alphabet. Every item in an array has an index (the number in the array it is; 
// e.g,. 'a' has an index of 0 and 'b' has an index of 1).
const alphabet = [a,b,c,d...]

// you can therefore use the index to return specific letters without referencing the letters
return ...

}

Here is more information on how to use Arrays in Javascript, specifically how to access an array item by it's index.

CodePudding user response:

You can use ASCII/utf8 codes for letters and convert them.

Link to these codes: http://sticksandstones.kstrom.com/appen.html

Link to fromCharCode MDN method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode

Step 1. Make an array of the codes that match the letters "happy birthday" (also the space code "32").

Step 2. Make an empty variable to which you will attach the letters you convert.

Step 3. Loop through the array of codes and add them to your empty variable.

Step 4. Return your variable, log your function.

CodePudding user response:

The following expression evaluates to the string "happy birthday" in Node.js:

Buffer.from("aGFwcHkgYmlydGhkYXk=", "base64").toString()

In a browser, you can instead use

atob("aGFwcHkgYmlydGhkYXk=")
  • Related