Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Alternate Capitalization

// Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
// For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.
// The input will be a lowercase string with no spaces.

const capitalize = s => {
  s = s.split('')
  let finalArr = []
  let i = 0
  while(i < 2){
    let str = ''
    s.forEach((letter, index) => {
      if(i % 2 === 0 && index % 2 === 0) str += letter.toUpperCase()
      else if(i % 2 !== 0 && index % 2 !== 0) str += letter.toUpperCase()
      else str += letter
    } )
    
    finalArr.push(str)
    i++
  }
  return finalArr
};
console.log(capitalize("abcdef"))

// With love @kouqhar
Comment

PREVIOUS NEXT
Code Example
Javascript :: Elementor Hide Sticky Header on Scroll Down - Show on Scroll Up 
Javascript :: how to generate random 6 digit charecter in js for coupon 
Javascript :: phaser place on line 
Javascript :: phaser random triangle 
Javascript :: phaser 60 fps animation test 
Javascript :: phaser enable pixel art 
Javascript :: phaser play animation after delay 
Javascript :: get lat long react native 
Javascript :: como usar variables en selector jquery 
Javascript :: on refresh action set position rainmeter 
Javascript :: after end time run function 
Javascript :: get random item in array 
Javascript :: find numeric Unicode value of the character with charCodeAt() method 
Javascript :: how to get params from function js 
Javascript :: Using the forEach function In JavaScript 
Javascript :: what is so called abstractions in javascript 
Javascript :: js object filter by keys 
Javascript :: js filter example 
Javascript :: codesandbox react emet 
Javascript :: js function arguments 
Javascript :: js functional ajax requests 
Javascript :: vanilla js 
Javascript :: javascript document get by attribute 
Javascript :: javascript sort object by value descending 
Javascript :: disable js in chrome dev tools 
Javascript :: spotify player react 
Javascript :: express get port from request 
Javascript :: reference data types in javascript 
Javascript :: buffer image 
Javascript :: how to disable previous date in datepicker using angular 6 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =