DEV Community

Discussion on: JavaScript Code Daily Challenge #5

Collapse
 
lakshyatyagi24 profile image
Lakshya Tyagi
function getIndex(char) {
    var alphabet = ['a', 'b', 'c', 'd', 'e',
        'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y',
        'z'];

    return alphabet.indexOf(char);
}
Enter fullscreen mode Exit fullscreen mode
function designerPdfViewer(h, word) {
    let wordHeight = [], maxVal = 0;
    for (let i = 0; i < word.length; i++) {
        wordHeight.push(h[getIndex(word[i])]);
    }

    maxVal = Math.max(...wordHeight);
    return (word.length * maxVal);

}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nahlagalal profile image
NahlaGalal

We can get index of character by using charCodeAt()

function designerPdfViewer(h, word) {
    let max = 0;
    for(let i in word) {
        const charIndex = word.charCodeAt(i) - 97;
        if(h[charIndex] > max) max = h[charIndex];
    }
    return max * word.length;

}
Enter fullscreen mode Exit fullscreen mode