Write a hello world program in javascript

How to write hello world program in node.js?

console.log("hello world");
5 Likes

No no no no. This is too easy …

const helloWorld = (() => {
  let str = '';
  const charCodes = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100];
  charCodes.forEach(code => str += String.fromCharCode(code));
  return str;
})();
console.log(helloWorld);

But this is JavaScript :wink: ops node.JS … ops not really a difference …

2 Likes

You could just do that like this:

console.log(String.fromCharCode(72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100));
2 Likes

But then where would the fun be …

2 Likes

You can create a Hello world program by creating a function that returns an array that contains all printable characters found in ASCII for reusability:

function getASCII() {
    // create a new array to store the ASCII characters
    let result = [];

    // loop through the code numbers for printable ASCII characters
    for (let i = 32 /* ASCII code for " " */ ; i <= 126 /* ASCII code for "~" */ ; i++) {
        result.push(String.fromCharCode(i);
    }

    // returns the ASCII codes
    return asciiCodes;
}

After that, you can create a function that returns a list of codes from a string:

function getCodesFromString(string) {
    // get the ASCII characters
    const asciiCodes = getASCII();
    
    // create an array that allows the function to easily return the array when processing is done
    let result = [];

    // loops through the string to find the ASCII codes for the string
    for (character of string) {
        // checks if the character exists in ASCII
        if (asciiCodes.indexOf(character) > 0) {
            // adds the code to the result array
            result.push(asciiCodes.indexOf(character));
        else {
            // adds the code for "?" in place of the unknown character
            result.push(asciiCodes.indexOf('?');
        }
    }

    // returns the ASCII codes
    return result;
}

Then, create a function to read the ASCII codes and turn them into a string:

function readASCIICodes(codes) {
    // create an empty string to allow the function to store its result for processing
    let result = '';
    // read every code and adds it to the result
    for (code of codes) {
        result += String.fromCharCode(code);
    }
    
    // returns the string
    return result;
}

You can then combine the functions together by creating a single function:

function createString(characters) {
    // get the codes for the characters
    const codes = getCodesFromString(characters);
    
    // create the string from the codes and returns it
    return readASCIICodes(codes);
}

Afterwards, you can display the string using console.log():

console.log(createString("Hello World!"));

This should print Hello World! into the console.

Hope this helps!

3 Likes

Use this code right here:

// evil interpolating polynomial coming up
let f = (x) => {
    return -(37*x**11)/570240 + (18967*x**10)/3628800 - (19153*x**9)/103680 + (455699*x**8)/120960 - (494327*x**7)/10080 + (73543171*x**6)/172800 - (258914477*x**5)/103680 + (223081373*x**4)/22680 - (655059671*x**3)/25920 + (1006150093*x**2)/25200 - (95429329*x)/2772 + 12125
}
let i = 1;
let chars = [];
for(; i<=12; i+=1){
    chars.push(String.fromCharCode(Math.round(f(i))));
}
console.log(chars.join(''))

I swear this works, try it yourself

2 Likes
//obfuscation moment :)
let a = "h";
let b = "e";
let c = "l";
let d = "l";
let e = "o";
let f  = "w";
let g = "o";
let h = "r";
let i = "l";
let j = "d";
console.log(a);
setTimeout(() => {
console.log(b);
setTimeout(() => {
console.log(c);
setTimeout(() => {
console.log(d);
setTimeout(() => {
console.log(e);
setTimeout(() => 
console.log(f);
setTimeout(() => {
console.log(g);
setTimeout(() => {
console.log(h);
setTimeout(() => {
console.log(i);
setTimeout(() => {
console.log(j);
}, "1000");
}, "1000");
}, "1000");
}, "1000");
}, "1000");
}, "1000");
},"1000");
},"1000");
}, "1000");

Javascipt Nesting Doll

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.