My first code! 😁

Hey there, @Grasshopper_Ben. I made a JavaScript program that checks if a string match the string sequence.

Here is a screenshot:





Output:
Randomly select and print the word from a variable str which stores an array of words and randomly select and print a word using pickRandom() function, then print a string sequence (Letter β€˜e’ at indices 0 and 5, letter β€˜n’ at index 1, letter β€˜c’ at index 2, letter β€˜o’ at index 3 and letter β€˜d’ at index 4) using the for...in loop. A function checkLetter is declared, the if statement inside a function checks if a word match the string sequence. If so then returns true, otherwise returns false. The checkLetter() function is called inside a console.log() statement and will prints out true if a string match the string sequence, or false if a string does not match the string sequence.

Thanks,
Pummarin

Why not share the link of the code so that we can see the code more clearly and see it in action?

Sure, @Joandy! Here is the code:

let str = pickRandom(['code', 'array', 'javascript', 'embed', 'index', 'variable', 'object', 'function', 'method', 'operator', 'ternary', 'assignment', 'output', 'boolean', 'string', 'number', 'encode']);
let obj = {
    e: [0, 5],
    n: [1],
    c: [2],
    o: [3],
    d: [4]
};
function checkLetter(word, letter) {
    if (word.includes(letter)) {
        return true;
    }
    return false;
};
console.log(str);
for (var strObj in obj) {
    console.log(strObj + ': ' + '[ ' + obj[strObj].join(', ') + ' ]');
}
console.log(checkLetter(str, strObj));

Hope you can clearly read my code.
Pummarin :slightly_smiling_face:

Nice, I’ll have to copy and paste the code to see it in action.

Edit: That’s a cool code.

1 Like