JS Complementary DNA & Monotone travel & Showing X to Y of Z Products.

1.Complementary DNA

Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.

If you want to know more http://en.wikipedia.org/wiki/DNA

In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You have function with one side of the DNA (string, except for Haskell); you need to get the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).

DNAStrand ("ATTGC") # return "TAACG"

DNAStrand ("GTAT") # return "CATA"
//No.1
function DNAStrand(dna){
    return dna.replace(/./g,function(c){
        return DNAStrand.pairs[c];
    })
}
DNAStrand.pairs = {
    A:'T',
    T:'A',
    G:'C',
    C:'G',
}
DNAStrand("ATTGCG");
//No.2
pairs = {
    'A':'T',
    'T':'A',
    'G':'C',
    'G':'C',
}
function DNAStrand(dna){
    return dna.split('').map(function(c){return pairs[c]}).join('');
}

2.Monotone travel

Story

Peter lives on a hill, and he always moans about the way to his home. "It's always just up. I never get a rest". But you're pretty sure that at least at one point Peter's altitude doesn't rise, but fall. To get him, you use a nefarious plan: you attach an altimeter to his backpack and you read the data from his way back at the next day.

Task

You're given a list of compareable elements:

heights = [h1, h2, h3, …, hn]
Your job is to check whether for any x all successors are greater or equal to x.

isMonotone([1,2,3]) == true
isMonotone([1,1,2]) == true
isMonotone([1])     == true
isMonotone([3,2,1]) == false
isMonotone([3,2,2]) == false
If the list is empty, Peter has probably removed your altimeter, so we cannot prove him wrong and he's still right:

isMonotone([])     == True
//No.1
var isMonotone = function(arr){

}
//No.2
var isMonotone = function(arr){
  for (var i = 0; i < arr.length-1; i++) {
      if (arr[i]>arr[i+1]) {return false};
  }
  return true;
}

isMonotone([1,2,3]);

3.Showing X to Y of Z Products.

A category page displays a set number of products per page, with pagination at the bottom allowing the user to move from page to page.

Given that you know the page you are on, how many products are in the category in total, and how many products are on any given page, how would you output a simple string showing which products you are viewing..

examples

In a category of 30 products with 10 products per page, on page 1 you would see

'Showing 1 to 10 of 30 Products.'

In a category of 26 products with 10 products per page, on page 3 you would see

'Showing 21 to 26 of 26 Products.'

In a category of 8 products with 10 products per page, on page 1 you would see

'Showing 1 to 8 of 8 Products.'
//My:
var paginationText = function(pageNumber, pageSize, totalProducts){
  var a="Showing ";
  console.log((pageNumber-1)*pageSize+1);
a+=(pageNumber-1)*pageSize+1;
a+=' to ';
if (pageNumber*pageSize < totalProducts) {  
    console.log(pageNumber*pageSize);
    a+=pageNumber*pageSize;
  }else {
    console.log(totalProducts);
    a+=totalProducts;
  }
  a+=" of";
  a+=totalProducts;
  a+=" Products";
  return a;
}
//NO.1 用Math.min !!!!或者 A?B:C 老是忘记
var paginationText = function(pageNumber, pageSize, totalProducts){
  return 'Showing ' + (((pageNumber - 1) * pageSize) + 1) + ' to ' + Math.min(pageSize * pageNumber, totalProducts) + ' of ' + totalProducts + ' Products.';
}

你可能感兴趣的:(javascrpit)