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

本文探讨了JavaScript编程中的三个主题:如何处理互补DNA的算法,实现单调路径遍历的方法,以及在电商场景中展示X到Y个总为Z的产品策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值