Q:

Write a JavaScript program to extract the first half of a string of even length

0

 Write a JavaScript program to extract the first half of a string of even length.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function first_half (str) {
  if (str.length % 2 == 0) {
    return str.slice(0, str.length / 2);
  }
  return str;
}
console.log(first_half("Python"));  
console.log(first_half("JavaScript")); 
console.log(first_half("PHP"));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now