SEI-ATL / UNIT_2

This repo is for all things Unit 2
0 stars 3 forks source link

[unit 2] Challenge Problems | 11/16/2020 #18

Closed romebell closed 3 years ago

romebell commented 4 years ago

Challenge 1

Write a function mindPsAndQs(str) that accepts a string of uppercase letters. The function should return the length of the longest consecutive streak of the letters 'P' and 'Q'.

Hint: Use two variables. One to track the length of the current streak and another to track the length of the longest streak so far. Think of using a strategy similar to maxValue. This can also be solved using a single loop!

Nested loops not needed!

Examples:

mindPsAndQs('BOOTCAMP'); // => 1
mindPsAndQs('APCDQQPPC'); // => 4
mindPsAndQs('PQPQ'); // => 4
mindPsAndQs('PPPXQPPPQ'); // => 5
function mindPsAndQs(str) {

}
NikkiHmltn commented 4 years ago
function evenCaps(sentence) {
    let string = sentence.split('');
    for(let i = 0; i < string.length; i++) {
        if (i % 2 === 0) {
            string[i] = string[i].toUpperCase();
        }
    }

    return string.join('');
}
console.log(evenCaps("Tom got a small piece of pie"));
fmuwanguzi commented 4 years ago
function evenCaps (sentence) {
    let sentenceArray = [];
    sentenceArray = sentence.split('');
    let newArray = [];
    for (let i = 0; i < sentenceArray.length; i++) {
        if (i % 2 === 0) {
            newArray.push(sentenceArray[i].toUpperCase());
        } else {
            newArray.push(sentenceArray[i])
        }
    }
    let newSentence = newArray.join('');
    return newSentence
}
codebypaul commented 4 years ago

const evenCaps = (sentence) =>{ newStr = '' for (let i = 0; i < sentence.length; i++){ if (i%2 === 0) newStr += sentence[i].toUpperCase() else newStr += sentence[i] } console.log(newStr) } evenCaps('this is my sentence')

edgerees commented 4 years ago
function evenCaps (sentence) {
    let sentenceArray = [];
    sentenceArray = sentence.split('');
    let newArray = [];
    for (let i = 0; i < sentenceArray.length; i++) {
        if (i % 2 === 0) {
            newArray.push(sentenceArray[i].toUpperCase());
        } else {
            newArray.push(sentenceArray[i])
        }
    }
    let newSentence = newArray.join('');
    return newSentence
}