1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
SCORPION-xisa [38]
3 years ago
13

Write a function string middle(string str) that returns a string containing the middle character in str if the length of str is

odd, or the two middle characters if the length is even. For example, middle ("middle") returns "dd".
Computers and Technology
1 answer:
Katen [24]3 years ago
6 0

Answer:

function getMiddle(s) {

return s.length % 2 ? s.substr(s.length / 2, 1) : s.substr((s.length / 2) - 1, 2);

}

// I/O stuff

document.getElementById("submit").addEventListener("click", function() {

input = document.getElementById("input").value;

document.getElementById("output").innerHTML = getMiddle(input);

});

Explanation:

// >>> is an unsigned right shift bitwise operator. It's equivalent to division by 2, with truncation, as long as the length of the string does not exceed the size of an integer in Javascript.

// About the ~ operator, let's rather start with the expression n & 1. This will tell you whether an integer n is odd (it's similar to a logical and, but comparing all of the bits of two numbers). The expression returns 1 if an integer is odd. It returns 0 if an integer is even.

// If n & 1 is even, the expression returns 0.

// If n & 1 is odd, the expression returns 1.

// ~n & 1 inverts those two results, providing 0 if the length of the string is odd, and 1 if the length of the sting is even. The ~ operator inverts all of the bits in an integer, so 0 would become -1, 1 would become -2, and so on (the leading bit is always the sign).

// Then you add one, and you get 0+1 (1) characters if the length of the string is odd, or 1+1 (2) characters if the length of the string is even.

You might be interested in
What is the equivalent of film speed in digital cameras?
elena55 [62]
The equivalent of film speed in a digital camera is going to be sensitivity. This will make the correct answer B. 
3 0
3 years ago
Read 2 more answers
Tabs are usually set ________ to the right of the left margin.
gayaneshka [121]
Ur answer is: a-half-inch
6 0
3 years ago
Read 2 more answers
In your own words, explain the FNAF timeline
tatiyna

Answer:

see shawty problem is, I havent had that phase yet, my cousin would be able to answer this tho

3 0
3 years ago
West Point Bridge Designer | Does anyone know a way to make this bridge cost less?
e-lub [12.9K]
Make the zig zag part more spaced out
4 0
3 years ago
Risk of new technology is NOT evaluated by asking questions during which phase?
neonofarm [45]

Answer:

the answer is D

Explanation:

hope this helps :)

4 0
3 years ago
Other questions:
  • Which resource helps a user access a cloud service?
    8·1 answer
  • What part of a check is the LEAST important?
    14·2 answers
  • Write a program that prints the following text: In C, lowercase letters are significant. main() is where program execution begin
    14·1 answer
  • A(n) ________ moves over the spinning platters to retrieve data from the hard disk.
    6·1 answer
  • In rolling a die four times, what is the odds that at least one 5 would appear?
    6·1 answer
  • In the Mouse Properties window, you can?
    8·1 answer
  • The best way to ensure the accuracy and safety of your accounts is to:
    10·1 answer
  • Why is it difficult to convince top management to commit funds to develop and implement a Strategic Information System
    13·1 answer
  • If i hit the delete key three times what will be left
    12·1 answer
  • What are 3 customizations that can be done using the header/footer section in the customize reports tray?.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!